pub struct Color(/* private fields */);
Implementations§
Source§impl Color
impl Color
Sourcepub fn from(color: &str) -> Result<Color, ColorError>
pub fn from(color: &str) -> Result<Color, ColorError>
create Color from str.
§Arguments
color_str
- Specify the color, ex:#FF00AA
,#FF00AA80
,rgb(129,45,78)
,rgba(129,45,78, 0.8)
,hsl(120, 45%, 90%)
,hsla(120, 45%, 90%, 0.5)
,hsv(120, 60%, 80%)
,cmyk(100,40,70,90)
not case sensitive.
§Return
ColorResult<Color>
, if thecolor_str
format is invalid, it will be return ColorError::Format error, else return Color
§Example
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();
Sourcepub fn random() -> Self
pub fn random() -> Self
Generates a random Color
instance with random values for red, green, blue, and alpha channels.
Sourcepub fn from_hex_alpha(hex_alpha: &str) -> Result<Color, ColorError>
pub fn from_hex_alpha(hex_alpha: &str) -> Result<Color, ColorError>
Parses a hexadecimal color string with alpha channel and returns a Color
instance.
§Arguments
hex_alpha
- A hexadecimal color string with alpha channel in the format of “#RRGGBBAA”.
§Returns
A Color
instance if the input string is a valid hexadecimal color string with alpha channel, otherwise a ColorError::Format
error.
Sourcepub fn from_rgb_str(rgb: &str) -> Result<Color, ColorError>
pub fn from_rgb_str(rgb: &str) -> Result<Color, ColorError>
Sourcepub fn from_rgba_str(rgba: &str) -> Result<Color, ColorError>
pub fn from_rgba_str(rgba: &str) -> Result<Color, ColorError>
Sourcepub fn from_hsl_str(hsl: &str) -> Result<Color, ColorError>
pub fn from_hsl_str(hsl: &str) -> Result<Color, ColorError>
Sourcepub fn from_hsla_str(hsla: &str) -> Result<Color, ColorError>
pub fn from_hsla_str(hsla: &str) -> Result<Color, ColorError>
Sourcepub fn from_hsv_str(hsv: &str) -> Result<Color, ColorError>
pub fn from_hsv_str(hsv: &str) -> Result<Color, ColorError>
Sourcepub fn from_cmyk_str(cmyk: &str) -> Result<Color, ColorError>
pub fn from_cmyk_str(cmyk: &str) -> Result<Color, ColorError>
Sourcepub fn from_hsl(h: u32, s: f32, l: f32) -> Result<Color, ColorError>
pub fn from_hsl(h: u32, s: f32, l: f32) -> Result<Color, ColorError>
create Color from hsl
§Arguments
- h - Specify the Hue, the value need be between in 0 - 360
- s - Specify the Saturation, the value need be between in 0.0 - 1.0
- l - Specify teh Lightness, the value need be between in 0.0 - 1.0
§Example
use iColor::Color;
let color = Color::from_hsl(210, 0.79, 0.3).unwrap();
assert_eq!(color.to_hex(), "#104C88");
Sourcepub fn from_hsla(h: u32, s: f32, l: f32, a: f32) -> Result<Color, ColorError>
pub fn from_hsla(h: u32, s: f32, l: f32, a: f32) -> Result<Color, ColorError>
create Color from hsla
§Arguments
- h - Specify the Hue, the value need be between in 0 - 360
- s - Specify the Saturation, the value need be between in 0.0 - 1.0
- l - Specify teh Lightness, the value need be between in 0.0 - 1.0
- a - Specify the Alpha, the value need be between in 0.0 - 1.0
§Example
use iColor::Color;
let color = Color::from_hsla(210, 0.79, 0.3, 0.5).unwrap();
assert_eq!(color.to_hex(), "#87A5C3");
Sourcepub fn from_rgb(r: u8, g: u8, b: u8) -> Result<Color, ColorError>
pub fn from_rgb(r: u8, g: u8, b: u8) -> Result<Color, ColorError>
create Color from rgb
§Arguments
- r - Specify the Red, the value need be between in 0 - 255
- g - Specify the Green, the value need be between in 0 - 255
- b - Specify the Blue, the value need be between in 0 - 255
§Example
use iColor::Color;
let color = Color::from_rgb(16, 76, 136).unwrap();
assert_eq!(color.to_hex(), "#104C88");
Sourcepub fn from_rgba(r: u8, g: u8, b: u8, a: f32) -> Result<Color, ColorError>
pub fn from_rgba(r: u8, g: u8, b: u8, a: f32) -> Result<Color, ColorError>
create Color from rgba
§Arguments
- r - Specify the Red, the value need be between in 0 - 255
- g - Specify the Green, the value need be between in 0 - 255
- b - Specify the Blue, the value need be between in 0 - 255
- a - Specify the Alpha, the value need be between in 0.0 - 1.0
§Example
use iColor::Color;
let color = Color::from_rgba(16, 76, 136, 0.5).unwrap();
assert_eq!(color.to_hex(), "#87A5C3");
Sourcepub fn from_cmyk(c: f32, m: f32, y: f32, k: f32) -> Result<Color, ColorError>
pub fn from_cmyk(c: f32, m: f32, y: f32, k: f32) -> Result<Color, ColorError>
create Color from cmyk
§Arguments
- c - Specify the Cyan, the value need be between in 0.0 - 1.0
- m - Specify the Magenta, the value need be between in 0.0 - 1.0
- y - Specify the Yellow, the value need be between in 0.0 - 1.0
- k - Specify the Key (Black), the value need be between in 0.0 - 1.0
§Example
use iColor::Color;
let color = Color::from_cmyk(0.5, 0.2, 0.1, 0.1).unwrap();
assert_eq!(color.to_hex(), "#72B7CE");
Sourcepub fn from_hsv(h: u32, s: f32, v: f32) -> Result<Color, ColorError>
pub fn from_hsv(h: u32, s: f32, v: f32) -> Result<Color, ColorError>
create Color from hsv
§Arguments
- h - Specify the Hue, the value need be between in 0 - 360
- s - Specify the Saturation, the value need be between in 0.0 - 1.0
- v - Specify the Value, the value need be between in 0.0 - 1.0
§Example
use iColor::Color;
let color = Color::from_hsv(210, 0.44, 0.8).unwrap();
assert_eq!(color.to_hex(), "#729FCC");
Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Convert the color to a hexadecimal string representation. If the alpha channel is not 1.0, it will be computed with reg, green, and blue.
§Example
use iColor::Color;
let color = Color::from_rgb(255, 0, 0).unwrap();
assert_eq!(color.to_hex(), "#FF0000");
let color2 = Color::from_rgba(0,0,0,0.5).unwrap();
assert_eq!(color2.to_hex(), "#7F7F7F");
Sourcepub fn to_hex_alpha(&self) -> String
pub fn to_hex_alpha(&self) -> String
Convert the color to a hexadecimal string with alpha representation.
use iColor::Color;
let color = Color::from_rgba(255, 0, 0, 0.5).unwrap();
assert_eq!(color.to_hex_alpha(), "#FF00007F");
Sourcepub fn to_alpha_hex(&self) -> String
pub fn to_alpha_hex(&self) -> String
Convert the color to the format required by Excel, where the color format is usually #AARRGGBB, where AA is alpha
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_alpha_hex(), "#FFFF0000");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_alpha_hex(), "#7F000000");
Sourcepub fn to_rgb(&self) -> String
pub fn to_rgb(&self) -> String
Convert the color to a CSS RGB string representation. If the alpha channel is not 1.0, it will be computed with red, green, and blue.
§Example
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_rgb(), "rgb(255,0,0)");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_rgb(), "rgb(127,127,127)");
Sourcepub fn to_rgba(&self) -> String
pub fn to_rgba(&self) -> String
Convert the color to a CSS RGBA string representation.
§Example
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_rgba(), "rgba(255,0,0,1)");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_rgba(), "rgba(0,0,0,0.5)");
Sourcepub fn to_hsl(&self) -> String
pub fn to_hsl(&self) -> String
Convert the color to a CSS HSL string representation. If the alpha channel is not 1.0, it will be computed with red, green, and blue.
§Example
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_hsl(), "hsl(0,100%,50%)");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_hsl(), "hsl(0,0%,50%)");
Sourcepub fn to_hsla(&self) -> String
pub fn to_hsla(&self) -> String
Convert the color to a CSS HSLA string representation.A
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_hsla(), "hsla(0,100%,50%,1.0)");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_hsla(), "hsla(0,0%,0%,0.5)");
Sourcepub fn to_hsv(&self) -> String
pub fn to_hsv(&self) -> String
Convert the color to a CSS HSLA string representation.A
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_hsv(), "hsv(0,100%,100%)");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_hsv(), "hsv(0,0%,50%)");
Sourcepub fn to_cmyk(&self) -> String
pub fn to_cmyk(&self) -> String
Convert the color to a CSS cmyk string representation.A
use iColor::Color;
let color = Color::from("#FF0000").unwrap();
assert_eq!(color.to_cmyk(), "cmyk(0,100,100,0)");
let mut color2 = Color::from("#000").unwrap();
color2.set_alpha(0.5);
assert_eq!(color2.to_cmyk(), "cmyk(0,0,0,50)");
Sourcepub fn negate(&mut self) -> &mut Self
pub fn negate(&mut self) -> &mut Self
Inverts the color by subtracting each RGB component from 255 and inverting the alpha value.
Sourcepub fn fade(&mut self, ratio: f32) -> &mut Self
pub fn fade(&mut self, ratio: f32) -> &mut Self
Reduce the alpha value of the color by a given ratio.
§Arguments
ratio
- A float value between 0.0 and 1.0 representing the ratio by which to reduce the alpha value.
§Example
use iColor::Color;
let mut color = Color::from("#000").unwrap();
color.fade(0.5);
assert_eq!(color.to_rgba(), "rgba(0,0,0,0.5)");
color.fade(0.5);
assert_eq!(color.to_rgba(), "rgba(0,0,0,0.25)");
Sourcepub fn opaquer(&mut self, ratio: f32) -> &mut Self
pub fn opaquer(&mut self, ratio: f32) -> &mut Self
Increase the alpha value of the color by a given ratio.
§Arguments
ratio
- A float value between 0.0 and 1.0 representing the ratio by which to increase the alpha value.
§Example
use iColor::Color;
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)");
color.opaquer(0.5);
assert_eq!(color.to_rgba(), "rgba(0,0,0,0.67)");