Skip to content

Commit 6b22d1a

Browse files
author
Tony Crisci
committed
take out vpsearch
1 parent 7e56c50 commit 6b22d1a

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
authors = ["Tony Crisci <tony@dubstepdish.com>"]
55

66
[dependencies]
7-
vpsearch = "1.3.4"
87
lazy_static = "1.0"
98
bitflags = "1.0"
109

src/color.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate vpsearch;
2-
31
use std::collections::HashMap;
42

53
#[derive(Copy, Clone, Debug)]
@@ -82,15 +80,12 @@ pub fn color_from_triplet(name: &'static str, t: (u8, u8, u8)) -> Color {
8280
}
8381
}
8482

85-
impl vpsearch::MetricSpace for Color {
86-
type UserData = ();
87-
type Distance = f32;
88-
89-
fn distance(&self, other: &Self, _: &Self::UserData) -> Self::Distance {
90-
let dr = self.r - other.r;
91-
let dg = self.g - other.g;
92-
let db = self.b - other.b;
83+
impl Color {
84+
pub fn distance(&self, other: &Self) -> f32 {
85+
let dr = self.r - other.r;
86+
let dg = self.g - other.g;
87+
let db = self.b - other.b;
9388

94-
(dr*dr + dg*dg + db*db).sqrt()
95-
}
89+
dr*dr + dg*dg + db*db
90+
}
9691
}

src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ extern crate lazy_static;
33
#[macro_use]
44
extern crate bitflags;
55

6-
extern crate vpsearch;
7-
86
mod color;
97
mod color_names;
108
mod colors;
@@ -69,9 +67,17 @@ impl ColorNamer {
6967
pub fn name_hex_color(&self, hex: &str) -> Result<String, ColorError> {
7068
let color = color::color_from_hex("", &hex)?;
7169

72-
let vp = vpsearch::Tree::new(&self.colors);
73-
let (index, _) = vp.find_nearest(&color);
70+
let mut min_distance: f32 = std::f32::MAX;
71+
let mut closest_color = color;
72+
73+
for c in &self.colors {
74+
let distance = color.distance(c);
75+
if distance < min_distance {
76+
min_distance = distance;
77+
closest_color = *c;
78+
}
79+
}
7480

75-
Ok(String::from(self.colors[index].name))
81+
Ok(closest_color.name.to_string())
7682
}
7783
}

0 commit comments

Comments
 (0)