Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cie2000 to improve results #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ license = "MIT"
[dependencies]
lazy_static = "1.0"
bitflags = "1.0"
delta_e = "0.2"
lab = "0.7"

[dev-dependencies]
criterion = "0.1.2"


[[bench]]
name = "basic"
harness = false
73 changes: 13 additions & 60 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::collections::HashMap;

extern crate delta_e;
extern crate lab;


#[derive(Copy, Clone, Debug)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub lab: lab::Lab,
pub name: &'static str
}

lazy_static! {
static ref HEX_ALPHABETS: HashMap<char, u32> = {
static ref HEX_ALPHABETS: HashMap<char, u8> = {
let mut m = HashMap::new();
m.insert('0', 0);
m.insert('1', 1);
Expand Down Expand Up @@ -43,7 +45,7 @@ pub fn color_from_hex(name: &'static str, hex: &str) -> Result<Color, ColorError
hex.to_string().to_uppercase().chars().collect()
};

let mut value: Vec<u32> = vec![];
let mut value: [u8; 3] = [0, 0, 0];

if hex_vec.len() < 6 {
return Err(ColorError::InvalidColorError);
Expand All @@ -58,75 +60,26 @@ pub fn color_from_hex(name: &'static str, hex: &str) -> Result<Color, ColorError

let int1 = HEX_ALPHABETS.get(&hex_vec[i]).unwrap();
let int2 = HEX_ALPHABETS.get(&hex_vec[i+1]).unwrap();
value.insert(i / 2, int1 * 16 + int2);
value[i/2] = int1 * 16 + int2;

i += 2;
}

Ok(Color {
r: value[0] as f32,
g: value[1] as f32,
b: value[2] as f32,
name: name
lab: lab::Lab::from_rgb(&value),
name
})
}

pub fn color_from_triplet(name: &'static str, t: (u8, u8, u8)) -> Color {
Color {
r: t.0 as f32,
g: t.1 as f32,
b: t.2 as f32,
lab: lab::Lab::from_rgb(&[t.0, t.1, t.2]),
name,
}
}

impl Color {
/// http://www.easyrgb.com/en/math.php
pub fn to_lab(&self) -> (f32, f32, f32) {
let xyz_normalize = |c: f32| {
let c_normal = c / 255.0;
if c_normal > 0.04045 {
((c_normal + 0.055) / 1.055).powf(2.4)
} else {
c_normal / 12.92
}
};

let r = xyz_normalize(self.r);
let g = xyz_normalize(self.g);
let b = xyz_normalize(self.b);

let x = r * 0.4124 + g * 0.3576 + b * 0.1805;
let y = r * 0.2126 + g * 0.7152 + b * 0.0722;
let z = r * 0.0193 + g * 0.1192 + b * 0.9505;

let lab_normalize = |c: f32| {
if c > 0.008856 {
c.powf(1.0 / 3.0)
} else {
7.787 * c + 16.0 / 116.0
}
};

let lx = lab_normalize(x);
let ly = lab_normalize(y);
let lz = lab_normalize(z);

let l = 116.0 * ly - 16.0;
let a = 500.0 * (lx - ly);
let b = 200.0 * (ly - lz);

(l, a, b)
}

pub fn distance(&self, lab: (f32, f32, f32)) -> f32 {
let (sl, sa, sb) = self.to_lab();
let (ol, oa, ob) = lab;

let dl = sl - ol;
let da = sa - oa;
let db = sb - ob;

(dl*dl + da*da + db*db).sqrt()
pub fn distance(&self, color: Color) -> f32 {
delta_e::DE2000::new(self.lab, color.lab)
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ impl ColorNamer {

pub fn name_hex_color(&self, hex: &str) -> Result<String, ColorError> {
let color = color::color_from_hex("", &hex)?;
let lab = color.to_lab();

let mut min_distance: f32 = std::f32::MAX;
let mut closest_color = color;

for c in &self.colors {
let distance = c.distance(lab);
let distance = c.distance(color);
if distance < min_distance {
min_distance = distance;
closest_color = *c;
Expand Down