Skip to content

Commit cf81b09

Browse files
author
Tony Crisci
committed
cache lab calculation for color
1 parent 63f4d99 commit cf81b09

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/color.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn color_from_triplet(name: &'static str, t: (u8, u8, u8)) -> Color {
8282

8383
impl Color {
8484
/// http://www.easyrgb.com/en/math.php
85-
fn to_lab(&self) -> (f32, f32, f32) {
85+
pub fn to_lab(&self) -> (f32, f32, f32) {
8686
let xyz_normalize = |c: f32| {
8787
let c_normal = c / 255.0;
8888
if c_normal > 0.04045 {
@@ -119,9 +119,9 @@ impl Color {
119119
(l, a, b)
120120
}
121121

122-
pub fn distance(&self, other: &Self) -> f32 {
122+
pub fn distance(&self, lab: (f32, f32, f32)) -> f32 {
123123
let (sl, sa, sb) = self.to_lab();
124-
let (ol, oa, ob) = other.to_lab();
124+
let (ol, oa, ob) = lab;
125125

126126
let dl = sl - ol;
127127
let da = sa - oa;

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ impl ColorNamer {
6666

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

7071
let mut min_distance: f32 = std::f32::MAX;
7172
let mut closest_color = color;
7273

7374
for c in &self.colors {
74-
let distance = color.distance(c);
75+
let distance = c.distance(lab);
7576
if distance < min_distance {
7677
min_distance = distance;
7778
closest_color = *c;

0 commit comments

Comments
 (0)