Skip to content

Commit

Permalink
fix: color.hex() convert #rrggbbaa to #rgba
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed May 16, 2023
1 parent acbb8ea commit ba6bb8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/color/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ impl Color {
/// assert_eq!(color.hex(), "#ffffff80");
/// ```
pub fn hex(self) -> String {
if self.alpha == 1.0 {
simplify_hex(rgb2hex(self.rgb))
let hex = if self.alpha == 1.0 {
rgb2hex(self.rgb)
} else {
let (r, g, b) = self.rgb;
let color = (r, g, b, self.alpha);
rgba2hex(color)
}
rgba2hex((r, g, b, self.alpha))
};
simplify_hex(hex)
}
/// `rgb` string of the color
///
Expand Down Expand Up @@ -279,19 +279,19 @@ mod tests {
assert_eq!(color.lab(), "lab(100, 0, 0)");
assert_eq!(color.name(), "white");

let color = Color::new(0.0, 0.0, 0.0, 0.5);
assert_eq!(color.hex(), "#00000080");
let color = Color::new(0.0, 0.0, 0.0, 0.2);
assert_eq!(color.hex(), "#0003");
assert_eq!(color.rgb(), "rgb(0, 0, 0)");
assert_eq!(color.rgba(), "rgba(0, 0, 0, 0.5)");
assert_eq!(color.rgba(), "rgba(0, 0, 0, 0.2)");
assert_eq!(color.hsl(), "hsl(0, 0%, 0%)");
assert_eq!(color.hsla(), "hsla(0, 0%, 0%, 0.5)");
assert_eq!(color.hsla(), "hsla(0, 0%, 0%, 0.2)");
assert_eq!(color.hsv(), "hsv(0, 0%, 0%)");
assert_eq!(color.hsi(), "hsi(0, 0%, 0%)");
assert_eq!(color.hwb(), "hwb(0, 0%, 100%)");
assert_eq!(color.xyz(), "xyz(0.137931, 0.137931, 0.137931)");
assert_eq!(color.ycbcr(), "YCbCr(0, 128, 128)");
assert_eq!(color.lab(), "lab(0, 0, 0)");
assert_eq!(color.name(), "#00000080");
assert_eq!(color.name(), "#0003");

let color = Color::new(0.0, 128.0, 128.0, 1.0);
assert_eq!(color.hex(), "#008080");
Expand Down
20 changes: 19 additions & 1 deletion src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ pub(crate) fn vec2tuple(vec: Vec<f64>) -> (f64, f64, f64) {

/// Simplifies the hex code to a short hex code if possible.
pub(crate) fn simplify_hex(hex: String) -> String {
if hex.len() == 7 {
let hex_len = hex.len();
if hex_len == 7 || hex_len == 9 {
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 hex_len == 9 {
let a1 = hex.chars().nth(7).unwrap();
let a2 = hex.chars().nth(8).unwrap();

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

if r1 == r2 && g1 == g2 && b1 == b2 {
format!("#{}{}{}", r1, g1, b1)
} else {
Expand All @@ -50,5 +62,11 @@ mod tests {

let color = "#abcdef".to_string();
assert_eq!(simplify_hex(color), "#abcdef".to_string());

let color = "#00ff0033".to_string();
assert_eq!(simplify_hex(color), "#0f03".to_string());

let color = "#abcdef33".to_string();
assert_eq!(simplify_hex(color), "#abcdef33".to_string());
}
}

0 comments on commit ba6bb8a

Please sign in to comment.