5 个版本
0.1.4 | 2023年5月4日 |
---|---|
0.1.3 | 2023年4月24日 |
0.1.2 | 2023年4月21日 |
0.1.1 | 2023年4月21日 |
0.1.0 | 2023年4月21日 |
8 in #hsv
每月44次下载
35KB
434 代码行(不含注释)
一个相对通用的颜色格式转换工具,可以在 #RRGGBB, #RGB, #RRGGBBAA, hsl, hsla, hsv, cmyk 之间进行转换。
use iColor::Color;
let color = Color::from("#ff00aa").unwrap();
let color1 = Color::from("#f0a").unwrap();
let color2 = Color::from("#ff00aa80").unwrap();
let color3 = Color::from("rgb(129, 45, 78)").unwrap();
let color4 = Color::from("rgba(129, 45, 78, 0.8)").unwrap();
let color5 = Color::from("hsl(120, 45%, 90%)").unwrap();
let color6 = Color::from("hsla(120, 45%, 90%, 0.5)").unwrap();
let color7 = Color::from("hsv(120, 60%, 80%)").unwrap();
let color8 = Color::from("cmyk(100, 40, 70, 90)").unwrap();
颜色也可以通过以下方式创建
pub fn from_rgb(r: u8, g: u8, b: u8) -> ColorResult<Color>
pub fn from_rgba(r: u8, g: u8, b: u8, a: f32) -> ColorResult<Color>
pub fn from_hsl(h: u32, s: f32, l: f32) -> ColorResult<Color>
pub fn from_hsla(h: u32, s: f32, l: f32, a: f32) -> ColorResult<Color>
pub fn from_hsv(h: u32, s: f32, v: f32) -> ColorResult<Color>
pub fn from_cmyk(c: f32, m: f32, y: f32, k: f32) -> ColorResult<Color>
use iColor::Color;
let mut color = Color::from("#ff00aa").unwrap();
assert_eq!(color.to_hex(), "#FF00AA");
assert_eq!(color.to_rgb(), "rgb(255,0,170)");
assert_eq!(color.to_rgba(), "rgba(255,0,170,1)");
assert_eq!(color.to_hex_alpha(), "#FF00AAFF");
assert_eq!(color.to_alpha_hex(), "#FFFF00AA");
assert_eq!(color.to_hsl(),"hsl(320,100%,50%)");
assert_eq!(color.to_hsla(),"hsla(320,100%,50%,1.0)");
assert_eq!(color.to_hsv(),"hsv(320,100%,100%)");
assert_eq!(color.to_cmyk(),"cmyk(0,100,33,0)");
方法
//Determine whether the color is a dark color
pub fn is_dark(&self) -> bool
let color = Color::from("#000");
assert!(color.is_dark());
//Determine whether the color is a light color
pub fn is_light(&self) -> bool
let color = Color::from("#fff");
assert!(color.is_light());
// set alpha
pub fn set_alpha(&mut self, alpha: f32) -> &mut Self
let mut color1 = Color::from_rgb(100,125,25);
assert_eq!(color.to_rgba(), "rgba(100,125,25, 1.0)");
color1.set_alpha(0.4);
assert_eq!(color.to_rgba(), "rgba(100,125,25,0.4)");
// Inverts the color by subtracting each RGB component from 255 and inverting the alpha value.
pub fn negate(&mut self) -> &mut Self
let mut color1 = Color::from_rgba(25, 125,100, 0.3);
color1.negate();
assert_eq!(color1.to_rgba(), "rgba(200, 100, 125, 0.7)")
//Reduce the alpha value of the color by a given ratio.
pub fn fade(&mut self, ratio: f32) -> &mut Self
let mut color = Color::from("#000").unwrap();
color.fade(0.5);
assert_eq!(color.to_rgba(), "rgba(0,0,0,0.5)");
//Increase the alpha value of the color by a given ratio.
pub fn opaquer(&mut self, ratio: f32) -> &mut Self
let mut color = Color::from_rgba(0,0,0,0.3).unwrap();
color.opaquer(0.5);
assert_eq!(color.to_rgba(), "rgba(0,0,0,0.45)");
// Generates a random `Color`
pub fn random() -> Self
let color = Color::random();
依赖项
~2.3–3.5MB
~58K SLoC