Struct Color

Source
pub struct Color(/* private fields */);

Implementations§

Source§

impl Color

Source

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 the color_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();
Source

pub fn random() -> Self

Generates a random Color instance with random values for red, green, blue, and alpha channels.

Source

pub fn from_hex(hex: &str) -> Result<Color, ColorError>

Parses a hexadecimal color string and returns a Color instance.

§Arguments
  • hex - A hexadecimal color string in the format of “#RRGGBB” or “#RGB”.
§Returns

A Color instance if the input string is a valid hexadecimal color string, otherwise a ColorError::Format error.

Source

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.

Source

pub fn from_rgb_str(rgb: &str) -> Result<Color, ColorError>

Parses a string in the format of “rgb(R,G,B)” and returns a Color instance.

§Arguments
  • rgb - A string in the format of “rgb(R,G,B)”.
§Returns

A Color instance if the input string is a valid RGB string, otherwise a ColorError::Format error.

Source

pub fn from_rgba_str(rgba: &str) -> Result<Color, ColorError>

Parses a string in the format of “rgba(R,G,B,A)” and returns a Color instance.

§Arguments
  • rgba - A string in the format of “rgba(R,G,B,A)”.
§Returns

A Color instance if the input string is a valid RGB string, otherwise a ColorError::Format error.

Source

pub fn from_hsl_str(hsl: &str) -> Result<Color, ColorError>

Parses a string in the format of “hsl(H,S,L)” and returns a Color instance.

§Arguments
  • hsl - A string in the format of “hsl(H,S,L)”.
§Returns

A Color instance if the input string is a valid RGB string, otherwise a ColorError::Format error.

Source

pub fn from_hsla_str(hsla: &str) -> Result<Color, ColorError>

Parses a string in the format of “hsla(H,S,L,A)” and returns a Color instance.

§Arguments
  • hsla - A string in the format of “hsla(H,S,L,A)”.
§Returns

A Color instance if the input string is a valid RGB string, otherwise a ColorError::Format error.

Source

pub fn from_hsv_str(hsv: &str) -> Result<Color, ColorError>

Parses a string in the format of “hsv(H,S,V)” and returns a Color instance.

§Arguments
  • hsv - A string in the format of “hsv(H,S,V)”.
§Returns

A Color instance if the input string is a valid RGB string, otherwise a ColorError::Format error.

Source

pub fn from_cmyk_str(cmyk: &str) -> Result<Color, ColorError>

Parses a string in the format of “cmyk(C,M,Y,K)” and returns a Color instance.

§Arguments
  • cmyk - A string in the format of “cmyk(C,M,Y,K)”.
§Returns

A Color instance if the input string is a valid RGB string, otherwise a ColorError::Format error.

Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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)");
Source

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)");
Source

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%)");
Source

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)");
Source

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%)");
Source

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)");
Source

pub fn set_alpha(&mut self, alpha: f32) -> &mut Self

Set the alpha value of the color.

§Arguments
  • alpha - A float value between 0.0 and 1.0 representing the alpha value of the color.
§Example
use iColor::Color;

let mut color = Color::from("#000").unwrap();
color.set_alpha(0.5);
assert_eq!(color.to_hsl(), "hsl(0,0%,50%)");
Source

pub fn is_dark(&self) -> bool

Determine whether the color is a dark color

Source

pub fn is_light(&self) -> bool

Determine whether the color is a light color

Source

pub fn negate(&mut self) -> &mut Self

Inverts the color by subtracting each RGB component from 255 and inverting the alpha value.

Source

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)");
Source

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)");

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Color

Source§

fn default() -> Color

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Color

Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V