Skip to content

Commit

Permalink
feat!: hex() will simplifies hex code if possible.
Browse files Browse the repository at this point in the history
It will convert #rrggbb to #rgb.
  • Loading branch information
JiatLn committed May 16, 2023
1 parent b12a967 commit 91ae87f
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ You can use the `hex`, `rgb`, `rgba`, `hsl`, `hsla`, `hsv`, `hsi`, `hwb`, `cmyk`
use color_art::color;

let color = color!(#ffff00);
color.hex(); // "#ffff00"
color.hex(); // "#ff0"
color.rgb(); // "rgb(255, 255, 0)"
color.rgba(); // "rgba(255, 255, 0, 1)"
color.hsl(); // "hsl(60, 100%, 50%)"
Expand Down
2 changes: 1 addition & 1 deletion src/color/color_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod tests {
assert_eq!(color.hsv(), "hsv(60, 100%, 100%)");

let color = color!(#f00);
assert_eq!(color.hex(), "#ff0000");
assert_eq!(color.hex(), "#f00");

let color = color!(#abcdef);
assert_eq!(color.hex(), "#abcdef");
Expand Down
4 changes: 2 additions & 2 deletions src/color/from_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::from_num(0xff3399).unwrap();
/// assert_eq!(color.hex(), "#ff3399");
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_num(num: u32) -> Result<Self> {
if num > 0xffffff {
Expand All @@ -27,7 +27,7 @@ impl Color {
#[test]
fn test_color_from_num() {
let color = Color::from_num(0xff3399).unwrap();
assert_eq!(color.hex(), "#ff3399");
assert_eq!(color.hex(), "#f39");

let color = Color::from_num(777).unwrap();
assert_eq!(color.hex(), "#000309");
Expand Down
20 changes: 10 additions & 10 deletions src/color/from_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::from_rgb(255, 51, 153).unwrap();
/// assert_eq!(color.hex(), "#ff3399");
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_rgb<T>(r: T, g: T, b: T) -> Result<Self> where T: Into<f64> {
let r = r.into();
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::from_hsl(330.0, 1.0, 0.6).unwrap();
/// assert_eq!(color.hex(), "#ff3399");
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Self> {
ColorSpace::HSL.valid(&vec![h, s, l])?;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::from_cmyk(0.0, 0.8, 0.4, 0.0).unwrap();
/// assert_eq!(color.hex(), "#ff3399");
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Result<Self> {
ColorSpace::CMYK.valid(&vec![c, m, y, k])?;
Expand All @@ -101,7 +101,7 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::from_hex("#ff3399").unwrap();
/// assert_eq!(color.hex(), "#ff3399");
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_hex(hex_str: &str) -> Result<Self> {
ColorSpace::valid_hex(hex_str)?;
Expand All @@ -122,7 +122,7 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::from_name("yellow").unwrap();
/// assert_eq!(color.hex(), "#ffff00");
/// assert_eq!(color.hex(), "#ff0");
///
/// let color = Color::from_name("水绿").unwrap();
/// assert_eq!(color.hex(), "#8cc269");
Expand All @@ -143,10 +143,10 @@ mod tests {
#[test]
fn test_color_from_rgb() {
let color = Color::from_rgb(255, 51, 153).unwrap();
assert_eq!(color.hex(), "#ff3399");
assert_eq!(color.hex(), "#f39");

let color = Color::from_rgb(255.0, 51.0, 153.0).unwrap();
assert_eq!(color.hex(), "#ff3399");
assert_eq!(color.hex(), "#f39");
}

#[test]
Expand All @@ -158,7 +158,7 @@ mod tests {
#[test]
fn test_color_from_hsl() {
let color = Color::from_hsl(330.0, 1.0, 0.6).unwrap();
assert_eq!(color.hex(), "#ff3399");
assert_eq!(color.hex(), "#f39");
}

#[test]
Expand All @@ -170,10 +170,10 @@ mod tests {
#[test]
fn test_color_from_cmyk() {
let color = Color::from_cmyk(0.0, 0.8, 0.4, 0.0).unwrap();
assert_eq!(color.hex(), "#ff3399");
assert_eq!(color.hex(), "#f39");

let color = Color::from_cmyk(0.2, 0.8, 0.0, 0.0).unwrap();
assert_eq!(color.hex(), "#cc33ff");
assert_eq!(color.hex(), "#c3f");

let color = Color::from_cmyk(2.0, 0.0, 0.0, 1.0);
assert!(color.is_err());
Expand Down
12 changes: 6 additions & 6 deletions src/color/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,22 @@ mod tests {
#[test]
fn test_color_from_cmyk_str() {
let color = Color::from_str("cmyk(0, 100%, 100%, 0)").unwrap();
assert_eq!(color.hex(), "#ff0000");
assert_eq!(color.hex(), "#f00");

let color = Color::from_str("cmyk(100%, 0, 100%, 0)").unwrap();
assert_eq!(color.hex(), "#00ff00");
assert_eq!(color.hex(), "#0f0");

let color = Color::from_str("cmyk(100%, 100%, 0, 0)").unwrap();
assert_eq!(color.hex(), "#0000ff");
assert_eq!(color.hex(), "#00f");

let color = Color::from_str("cmyk(0, 0, 0, 100%)").unwrap();
assert_eq!(color.hex(), "#000000");
assert_eq!(color.hex(), "#000");

let color = Color::from_str("cmyk(0, 0, 0, 0)").unwrap();
assert_eq!(color.hex(), "#ffffff");
assert_eq!(color.hex(), "#fff");

let color = Color::from_str("cmyk(20%, 80%, 0, 0)").unwrap();
assert_eq!(color.hex(), "#cc33ff");
assert_eq!(color.hex(), "#c3f");

let color = Color::from_str("cmyk(35%, 0, 60%, 0)").unwrap();
assert_eq!(color.hex(), "#a6ff66");
Expand Down
21 changes: 12 additions & 9 deletions src/color/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
yuv::rgb2yuv,
},
data::name_of_hex,
helper::round,
helper::{ round, simplify_hex },
Color,
};

Expand All @@ -26,14 +26,14 @@ impl Color {
/// use color_art::Color;
///
/// let color = Color::new(255, 255, 255, 1.0);
/// assert_eq!(color.hex(), "#ffffff");
/// assert_eq!(color.hex(), "#fff");
///
/// let color = Color::new(255, 255, 255, 0.5);
/// assert_eq!(color.hex(), "#ffffff80");
/// ```
pub fn hex(self) -> String {
if self.alpha == 1.0 {
rgb2hex(self.rgb)
simplify_hex(rgb2hex(self.rgb))
} else {
let (r, g, b) = self.rgb;
let color = (r, g, b, self.alpha);
Expand Down Expand Up @@ -247,11 +247,14 @@ impl Color {
/// assert_eq!(color.name(), "#2a2a2a");
/// ```
pub fn name(self) -> String {
let hex = self.hex();

match name_of_hex(&hex) {
Some(name) => name.to_string(),
None => hex,
if self.alpha == 1.0 {
let hex = rgb2hex(self.rgb);
match name_of_hex(&hex) {
Some(name) => name.to_string(),
None => hex,
}
} else {
self.hex()
}
}
}
Expand All @@ -263,7 +266,7 @@ mod tests {
#[test]
fn test_stringify_color() {
let color = Color::new(255.0, 255.0, 255.0, 1.0);
assert_eq!(color.hex(), "#ffffff");
assert_eq!(color.hex(), "#fff");
assert_eq!(color.rgb(), "rgb(255, 255, 255)");
assert_eq!(color.rgba(), "rgba(255, 255, 255, 1)");
assert_eq!(color.hsl(), "hsl(0, 0%, 100%)");
Expand Down
2 changes: 1 addition & 1 deletion src/color_calc/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod tests {
assert_eq!(color.hex(), "#8ef6fa");

let color = blend(&c1, &c2, ColorDodge);
assert_eq!(color.hex(), "#ffffff");
assert_eq!(color.hex(), "#fff");

let color = blend(&c1, &c2, HardLight);
assert_eq!(color.hex(), "#e7f643");
Expand Down
2 changes: 1 addition & 1 deletion src/color_generator/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod tests {

let colors = vec![color!(#ffff00), color!(#ff0000), color!(#0000ff)];
let averaged_color = Color::average(&colors);
assert_eq!(averaged_color.hex(), "#aa5555");
assert_eq!(averaged_color.hex(), "#a55");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/color_generator/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod tests {
assert_eq!(color6.hex(), "#d2e1dd");

let color7 = Color::mix(&color1, &color2, 1.0).unwrap();
assert_eq!(color7.hex(), "#003366");
assert_eq!(color7.hex(), "#036");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/color_ops/darken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod tests {

let mut color = color!(#426105);
color.darken(0.5).unwrap();
assert_eq!(color.hex(), "#000000");
assert_eq!(color.hex(), "#000");

let mut color = color!(#80e619);
color.darken(0.2).unwrap();
Expand Down
34 changes: 34 additions & 0 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,37 @@ pub(crate) fn normalize_color(color: (f64, f64, f64)) -> (f64, f64, f64) {
pub(crate) fn vec2tuple(vec: Vec<f64>) -> (f64, f64, f64) {
(vec[0], vec[1], vec[2])
}

/// Simplifies the hex code to a short hex code if possible.
pub(crate) fn simplify_hex(hex: String) -> String {
if hex.len() == 7 {
let r1 = hex.chars().nth(1).unwrap();
let r2 = hex.chars().nth(2).unwrap();
let g1 = hex.chars().nth(3).unwrap();
let g2 = hex.chars().nth(4).unwrap();
let b1 = hex.chars().nth(5).unwrap();
let b2 = hex.chars().nth(6).unwrap();

if r1 == r2 && g1 == g2 && b1 == b2 {
format!("#{}{}{}", r1, g1, b1)
} else {
hex
}
} else {
hex
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_simplify_hex() {
let color = "#00ff00".to_string();
assert_eq!(simplify_hex(color), "#0f0".to_string());

let color = "#abcdef".to_string();
assert_eq!(simplify_hex(color), "#abcdef".to_string());
}
}
12 changes: 6 additions & 6 deletions tests/color_from_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ use color_art::Color;
fn test_color_from_space() {
// RGB
let color = Color::from_rgb(255, 255, 0).unwrap();
assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");

// RGBA
let color = Color::from_rgba(255, 255, 0, 0.5).unwrap();
assert_eq!(color.hex(), "#ffff0080");

// HSL
let color = Color::from_hsl(60.0, 1.0, 0.5).unwrap();
assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");

// HSV
let color = Color::from_hsv(60.0, 1.0, 1.0).unwrap();
assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");

// CMYK
let color = Color::from_cmyk(0.0, 0.0, 1.0, 0.0).unwrap();
assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");

// HEX
let color = Color::from_hex("#ffff00").unwrap();
assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");

// Name
let color = Color::from_name("yellow").unwrap();
assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");
}
2 changes: 1 addition & 1 deletion tests/color_stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use color_art::color;
fn test_color_stringify() {
let color = color!(#ffff00);

assert_eq!(color.hex(), "#ffff00");
assert_eq!(color.hex(), "#ff0");
assert_eq!(color.rgb(), "rgb(255, 255, 0)");
assert_eq!(color.rgba(), "rgba(255, 255, 0, 1)");
assert_eq!(color.hsl(), "hsl(60, 100%, 50%)");
Expand Down

0 comments on commit 91ae87f

Please sign in to comment.