Skip to content

Commit

Permalink
feat: improve vec_of fn
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed Aug 30, 2023
1 parent 7b72b22 commit 07ae49d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/color/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl FromStr for Color {

fn convert_color_vec_by_color_space(color_vec: &[f64], color_space: &ColorSpace) -> Vec<f64> {
match color_space {
ColorSpace::RGB | ColorSpace::RGBA | ColorSpace::HEX => color_vec.to_vec(),
ColorSpace::RGB | ColorSpace::RGBA | ColorSpace::HEX | ColorSpace::HEXA => {
color_vec.to_vec()
}
ColorSpace::HSI => conversion::hsi::hsi2rgb(color_vec),
ColorSpace::HSL => conversion::hsl::hsl2rgb(color_vec),
ColorSpace::HSV => conversion::hsv::hsv2rgb(color_vec),
Expand Down
11 changes: 6 additions & 5 deletions src/color/vec_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{conversion, Color, ColorSpace};
impl Color {
/// Get the color space vector of the color instance.
///
/// ⚗️ **Experimental**: This method is experimental and may change or be removed in the future.
/// ⚗️ **Experimental**: This method is experimental and may change frequently in the future.
///
/// # Examples
///
Expand All @@ -17,11 +17,12 @@ impl Color {
/// let vec = color.vec_of(ColorSpace::HSV);
/// assert_eq!(vec, vec![330.0, 0.8, 1.0]);
/// ```
pub fn vec_of(&self, color_space: ColorSpace) -> Vec<f64> {
pub fn vec_of(&self, color_space: impl Into<ColorSpace>) -> Vec<f64> {
let color = self.rgb.to_vec();
let color_space = color_space.into();
match color_space {
ColorSpace::RGB => color,
ColorSpace::RGBA => {
ColorSpace::RGB | ColorSpace::HEX => color,
ColorSpace::RGBA | ColorSpace::HEXA => {
let [r, g, b] = self.rgb;
vec![r, g, b, self.alpha]
}
Expand All @@ -40,7 +41,7 @@ impl Color {
ColorSpace::YUV => conversion::yuv::rgb2yuv(&color),
ColorSpace::YCbCr => conversion::ycbcr::rgb2ycbcr(&color),
ColorSpace::Lab => conversion::lab::rgb2lab(&color),
ColorSpace::HEX | ColorSpace::Unknown => todo!("not implemented yet"),
ColorSpace::Unknown => todo!("Unknown color space not yet implemented `vec_of`"),
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/color_vec_of.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use color_art::{color, ColorSpace};

#[test]
fn test_color_hex() {
let color = color!(#ff00ff);

let vec = color.vec_of("hex");
assert_eq!(vec, vec![255.0, 0.0, 255.0]);

let vec = color.vec_of(ColorSpace::HEXA);
assert_eq!(vec, vec![255.0, 0.0, 255.0, 1.0]);

let vec = color.vec_of(ColorSpace::RGB);
assert_eq!(vec, vec![255.0, 0.0, 255.0]);

let vec = color.vec_of("hsl");
assert_eq!(vec, vec![300.0, 1.0, 0.5]);
}

0 comments on commit 07ae49d

Please sign in to comment.