color equality only checks rgba value and normalizes channels
This commit is contained in:
parent
35da511860
commit
eb50381236
@ -18,6 +18,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
fmt::{self, Display},
|
fmt::{self, Display},
|
||||||
|
hash::{Hash, Hasher},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::value::Number;
|
use crate::value::Number;
|
||||||
@ -27,13 +28,27 @@ use num_traits::{One, Signed, ToPrimitive, Zero};
|
|||||||
|
|
||||||
mod name;
|
mod name;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct Color {
|
pub(crate) struct Color {
|
||||||
rgba: Rgba,
|
rgba: Rgba,
|
||||||
hsla: Option<Hsla>,
|
hsla: Option<Hsla>,
|
||||||
repr: String,
|
repr: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Color {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.rgba == other.rgba
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Color {}
|
||||||
|
|
||||||
|
impl Hash for Color {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.rgba.hash(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub const fn new_rgba(
|
pub const fn new_rgba(
|
||||||
red: Number,
|
red: Number,
|
||||||
@ -65,7 +80,7 @@ impl Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone)]
|
||||||
struct Rgba {
|
struct Rgba {
|
||||||
red: Number,
|
red: Number,
|
||||||
green: Number,
|
green: Number,
|
||||||
@ -73,6 +88,62 @@ struct Rgba {
|
|||||||
alpha: Number,
|
alpha: Number,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Rgba {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
if self.red != other.red
|
||||||
|
&& !(self.red >= Number::from(255) && other.red >= Number::from(255))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if self.green != other.green
|
||||||
|
&& !(self.green >= Number::from(255) && other.green >= Number::from(255))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if self.blue != other.blue
|
||||||
|
&& !(self.blue >= Number::from(255) && other.blue >= Number::from(255))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if self.alpha != other.alpha
|
||||||
|
&& !(self.alpha >= Number::one() && other.alpha >= Number::one())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Rgba {}
|
||||||
|
|
||||||
|
impl Hash for Rgba {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
if self.red > Number::from(255) {
|
||||||
|
255.hash(state);
|
||||||
|
} else {
|
||||||
|
self.red.hash(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.green > Number::from(255) {
|
||||||
|
255.hash(state);
|
||||||
|
} else {
|
||||||
|
self.green.hash(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.blue > Number::from(255) {
|
||||||
|
255.hash(state);
|
||||||
|
} else {
|
||||||
|
self.blue.hash(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.alpha > Number::one() {
|
||||||
|
1.hash(state);
|
||||||
|
} else {
|
||||||
|
self.alpha.hash(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Rgba {
|
impl Rgba {
|
||||||
pub const fn new(red: Number, green: Number, blue: Number, alpha: Number) -> Self {
|
pub const fn new(red: Number, green: Number, blue: Number, alpha: Number) -> Self {
|
||||||
Rgba {
|
Rgba {
|
||||||
@ -88,7 +159,7 @@ impl Rgba {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone)]
|
||||||
struct Hsla {
|
struct Hsla {
|
||||||
hue: Number,
|
hue: Number,
|
||||||
saturation: Number,
|
saturation: Number,
|
||||||
|
@ -623,3 +623,13 @@ test!(
|
|||||||
"a {\n color: blue(rgba(1.5, 1.5, 1.5, 1));\n}\n",
|
"a {\n color: blue(rgba(1.5, 1.5, 1.5, 1));\n}\n",
|
||||||
"a {\n color: 2;\n}\n"
|
"a {\n color: 2;\n}\n"
|
||||||
);
|
);
|
||||||
|
test!(
|
||||||
|
color_equality_named_and_hex,
|
||||||
|
"a {\n color: red==#ff0000;\n}\n",
|
||||||
|
"a {\n color: true;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
color_equality_named_and_hsla,
|
||||||
|
"a {\n color: hsla(0deg, 100%, 50%)==red;\n}\n",
|
||||||
|
"a {\n color: true;\n}\n"
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user