Expand description
An RGBA color.
See the module documentation for details.
Fields
r: u8
The red component of the color.
g: u8
The green component of the color.
b: u8
The blue component of the color.
a: u8
The alpha component of the color (0
is transparent, 255
is opaque).
Implementations
sourceimpl HexColor
impl HexColor
sourcepub const GRAY: HexColor = _
pub const GRAY: HexColor = _
Solid gray; American spelling of grey. RGBA is (128, 128, 128, 255)
.
sourcepub const GREY: HexColor = _
pub const GREY: HexColor = _
Solid grey; British spelling of gray. RGBA is (128, 128, 128, 255)
.
sourcepub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> HexColor
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> HexColor
Constructs a new RGBA value.
For creating just an RGB value instead, use HexColor::rgb
.
Examples
use hex_color::HexColor;
let red = HexColor::rgba(255, 0, 0, 255);
let translucent_red = HexColor::rgba(255, 0, 0, 128);
sourcepub const fn rgb(r: u8, g: u8, b: u8) -> HexColor
pub const fn rgb(r: u8, g: u8, b: u8) -> HexColor
Constructs a new RGB value. (The alpha channel defaults to u8::MAX
.)
For creating an RGBA value instead, use HexColor::rgba
.
Examples
use hex_color::HexColor;
let aqua = HexColor::rgb(0, 255, 255);
sourcepub const fn achromatic(v: u8) -> HexColor
pub const fn achromatic(v: u8) -> HexColor
Constructs a new achromatic RGB value. (The alpha channel defaults to
u8::MAX
.)
Examples
use hex_color::HexColor;
assert_eq!(HexColor::achromatic(128), HexColor::rgb(128, 128, 128));
Note: There is no “achromatic_alpha
” constructor or similar method.
Instead, it’s advised to chain HexColor::achromatic
with
HexColor::with_a
:
use hex_color::HexColor;
let transparent_dark_gray = HexColor::achromatic(64).with_a(128);
assert_eq!(transparent_dark_gray, HexColor::rgba(64, 64, 64, 128));
sourcepub fn random_rgb() -> Self
Available on crate features rand
and std
only.
pub fn random_rgb() -> Self
rand
and std
only.Constructs a new random RGB value through the rand
crate.
To generate a random RGBA value, use HexColor::random_rgba
instead.
Examples
use hex_color::HexColor;
println!("{}", HexColor::random_rgb());
sourcepub fn random_rgba() -> Self
Available on crate features rand
and std
only.
pub fn random_rgba() -> Self
rand
and std
only.Constructs a new random RGBA value through the rand
crate.
To generate a random RGB value, use HexColor::random_rgb
instead.
Examples
use hex_color::HexColor;
println!("{:#}", HexColor::random_rgba());
sourcepub fn parse(s: &str) -> Result<HexColor, ParseHexColorError>
pub fn parse(s: &str) -> Result<HexColor, ParseHexColorError>
Parses an RGB(A) hex code.
All parsing is case-insensitive. There are currently four parseable formats:
#RGB
#RRGGBB
#RGBA
#RRGGBBAA
To parse only a hexadecimal triplet, use parse_rgb
. Otherwise,
to parse only a hexadecimal quadruplet, use parse_rgba
.
Errors
Empty
when the input is empty.InvalidFormat
when the input is a malformed length or lacks a leading#
. If you suspect there might be whitespace in the input, consider callingstr::trim
first.InvalidDigit
when the format seems correct but one of the characters is an invalid hexadecimal digit.
Examples
use hex_color::HexColor;
let red = HexColor::parse("#F00")?;
assert_eq!(red, HexColor::rgb(0xFF, 0x00, 0x00));
let plum = HexColor::parse("#DDA0DD")?;
assert_eq!(plum, HexColor::rgb(0xDD, 0xA0, 0xDD));
let opaque_cyan = HexColor::parse("#0FFF")?;
assert_eq!(opaque_cyan, HexColor::rgba(0x00, 0xFF, 0xFF, 0xFF));
let translucent_azure = HexColor::parse("#F0FFFF80")?;
assert_eq!(translucent_azure, HexColor::rgba(0xF0, 0xFF, 0xFF, 0x80));
sourcepub fn parse_rgb(s: &str) -> Result<HexColor, ParseHexColorError>
pub fn parse_rgb(s: &str) -> Result<HexColor, ParseHexColorError>
Parses an RGB hex code.
All parsing is case-insensitive. There are currently two parseable formats:
#RGB
#RRGGBB
To parse only a hexadecimal quadruplet, use parse_rgba
. Otherwise,
to parse both hexadecimal triplets and quadruplets, use parse
.
Errors
Empty
when the input is empty.InvalidFormat
when the input is a malformed length or lacks a leading#
. If you suspect there might be whitespace in the input, consider callingstr::trim
first.InvalidDigit
when the format seems correct but one of the characters is an invalid hexadecimal digit.
Note: a valid RGBA input will return an InvalidFormat
. Use
parse_rgba
or parse
instead if that behavior is not desired.
Examples
use hex_color::HexColor;
let yellow = HexColor::parse_rgb("#FF0")?;
assert_eq!(yellow, HexColor::rgb(0xFF, 0xFF, 0x00));
let hot_pink = HexColor::parse_rgb("#FF69B4")?;
assert_eq!(hot_pink, HexColor::rgb(0xFF, 0x69, 0xB4));
sourcepub fn parse_rgba(s: &str) -> Result<HexColor, ParseHexColorError>
pub fn parse_rgba(s: &str) -> Result<HexColor, ParseHexColorError>
Parses an RGBA hex code.
All parsing is case-insensitive. There are currently two parseable formats:
#RGBA
#RRGGBBAA
To parse only a hexadecimal triplet, use parse_rgb
. Otherwise,
to parse both hexadecimal triplets and quadruplets, use parse
.
Errors
Empty
when the input is empty.InvalidFormat
when the input is a malformed length or lacks a leading#
. If you suspect there might be whitespace in the input, consider callingstr::trim
first.InvalidDigit
when the format seems correct but one of the characters is an invalid hexadecimal digit.
Note: a valid RGB input (without an alpha value) will return an
InvalidFormat
. Use parse_rgb
or parse
instead if that
behavior is not desired.
Examples
use hex_color::HexColor;
let transparent = HexColor::parse_rgba("#FFFF")?;
assert_eq!(transparent, HexColor::rgba(0xFF, 0xFF, 0xFF, 0xFF));
let translucent_gold = HexColor::parse_rgba("#FFD70080")?;
assert_eq!(translucent_gold, HexColor::rgba(0xFF, 0xD7, 0x00, 0x80));
sourcepub const fn from_u24(n: u32) -> HexColor
pub const fn from_u24(n: u32) -> HexColor
Converts any u32
in the range 0x0000_0000..=0x00FF_FFFF
to an RGB
value.
To convert any u32
to an RGBA value, use from_u32
instead.
Panics
Panics if debug_assertions
are enabled and the given value exceeds
0x00FF_FFFF
.
Examples
use hex_color::HexColor;
let pale_green = HexColor::from_u24(0x98FB98);
assert_eq!(pale_green, HexColor::rgb(0x98, 0xFB, 0x98));
sourcepub const fn from_u32(v: u32) -> HexColor
pub const fn from_u32(v: u32) -> HexColor
Converts any u32
to an RGBA value.
For converting a u32
in the range of 0x0000_0000..=0x00FF_FFFF
to
an RGB value, use from_u24
instead.
Examples
use hex_color::HexColor;
let navajo_white = HexColor::from_u32(0xFFDEADFF);
assert_eq!(navajo_white, HexColor::rgba(0xFF, 0xDE, 0xAD, 0xFF));
let translucent_violet = HexColor::from_u32(0xEE82EE80);
assert_eq!(translucent_violet, HexColor::rgba(0xEE, 0x82, 0xEE, 0x80));
sourcepub const fn to_u24(self) -> u32
pub const fn to_u24(self) -> u32
Converts a HexColor
into a u32
in the range 0x000000..=0xFFFFFF
,
discarding any possibly significant alpha value.
To convert this HexColor
into a u32
containing the alpha value as
well, use to_u32
.
Examples
use hex_color::HexColor;
let misty_rose = HexColor::rgb(0xFF, 0xE4, 0xE1);
assert_eq!(misty_rose.to_u24(), 0xFFE4E1);
// Again, note that the alpha value is lost in this conversion:
let translucent_navy = HexColor::rgba(0x00, 0x00, 0x80, 0x80);
assert_eq!(translucent_navy.to_u24(), 0x000080);
sourcepub const fn to_u32(self) -> u32
pub const fn to_u32(self) -> u32
Converts a HexColor
into a u32
.
To convert the HexColor
into a u32
with only the red, green, and
blue channels packed, use to_u24
.
Examples
use hex_color::HexColor;
let sea_shell = HexColor::rgb(0xFF, 0xF5, 0xEE);
assert_eq!(sea_shell.to_u32(), 0xFFF5EEFF);
// Unlike `to_u24` the alpha value is preserved
let translucent_navy = HexColor::rgba(0x00, 0x00, 0x80, 0x80);
assert_eq!(translucent_navy.to_u32(), 0x00008080);
sourcepub const fn to_be_bytes(self) -> [u8; 4]
pub const fn to_be_bytes(self) -> [u8; 4]
Converts a HexColor
into [r, g, b, a]
.
Examples
use hex_color::HexColor;
let color = HexColor::from_u32(0x89AB_CDEF);
assert_eq!(color.to_be_bytes(), [0x89, 0xAB, 0xCD, 0xEF]);
sourcepub const fn to_le_bytes(self) -> [u8; 4]
pub const fn to_le_bytes(self) -> [u8; 4]
Converts a HexColor
into [a, b, g, r]
.
Examples
use hex_color::HexColor;
let color = HexColor::from_u32(0x89AB_CDEF);
assert_eq!(color.to_le_bytes(), [0xEF, 0xCD, 0xAB, 0x89]);
sourcepub const fn with_r(self, r: u8) -> HexColor
pub const fn with_r(self, r: u8) -> HexColor
Constructs a new HexColor
derived from self
with the red component
of r
.
Examples
use hex_color::HexColor;
assert_eq!(HexColor::MIN.with_r(255), HexColor::rgba(255, 0, 0, 0));
sourcepub const fn with_g(self, g: u8) -> HexColor
pub const fn with_g(self, g: u8) -> HexColor
Constructs a new HexColor
derived from self
with the green component
of g
.
Examples
use hex_color::HexColor;
assert_eq!(HexColor::MIN.with_g(255), HexColor::rgba(0, 255, 0, 0));
sourcepub const fn with_b(self, b: u8) -> HexColor
pub const fn with_b(self, b: u8) -> HexColor
Constructs a new HexColor
derived from self
with the blue component
of b
.
Examples
use hex_color::HexColor;
assert_eq!(HexColor::MIN.with_b(255), HexColor::rgba(0, 0, 255, 0));
sourcepub const fn with_a(self, a: u8) -> HexColor
pub const fn with_a(self, a: u8) -> HexColor
Constructs a new HexColor
derived from self
with the alpha component
of a
.
Examples
use hex_color::HexColor;
assert_eq!(HexColor::MIN.with_a(255), HexColor::rgba(0, 0, 0, 255));
sourcepub const fn split(self) -> (u8, u8, u8, u8)
pub const fn split(self) -> (u8, u8, u8, u8)
Deconstructs a HexColor
into a tuple of its components: (r, g, b, a)
.
This primarily helps in cleaner deconstruction of HexColor
instances,
especially if the variable bindings aren’t the same as the struct'
s
fields.
Examples
Basic usage:
use hex_color::HexColor;
let slate_blue = HexColor::from_u24(0x6A5ACD);
let (red, green, blue, alpha) = slate_blue.split();
For contrast, here’s what it would look like otherwise; it’s not terrible, but en masse, it’s subjectively annoying:
use hex_color::HexColor;
let slate_blue = HexColor::from_u24(0x6A5ACD);
let HexColor {
r: red,
g: green,
b: blue,
a: alpha,
} = slate_blue;
sourcepub const fn add(self, rhs: HexColor) -> HexColor
pub const fn add(self, rhs: HexColor) -> HexColor
Adds two colors together.
Each component is added separately. The alpha component is ignored
entirely, always returning an RGB color (where alpha is the default
u8::MAX
).
Panics
Panics if any overflow occurs.
Examples
use hex_color::HexColor;
assert_eq!(HexColor::BLUE.add(HexColor::GREEN), HexColor::CYAN);
assert_eq!(HexColor::RED.add(HexColor::BLUE), HexColor::MAGENTA);
assert_eq!(HexColor::GREEN.add(HexColor::RED), HexColor::YELLOW);
sourcepub const fn checked_add(self, rhs: HexColor) -> Option<HexColor>
pub const fn checked_add(self, rhs: HexColor) -> Option<HexColor>
Checked color addition. Computes self + rhs
, returning None
if
overflow occurred.
Examples
use hex_color::HexColor;
let almost_white = HexColor::achromatic(254);
let one = HexColor::achromatic(1);
assert_eq!(almost_white.checked_add(one), Some(HexColor::WHITE));
assert_eq!(HexColor::WHITE.checked_add(one), None);
sourcepub const fn overflowing_add(self, rhs: HexColor) -> (HexColor, bool)
pub const fn overflowing_add(self, rhs: HexColor) -> (HexColor, bool)
Calculates self + rhs
.
Returns a tuple of the addition along with a boolean indicating whether any arithmetic overflow would occur. If an overflow would have occurred, then the wrapped value is returned.
Examples
use hex_color::HexColor;
let almost_white = HexColor::achromatic(254);
let one = HexColor::achromatic(1);
assert_eq!(
almost_white.overflowing_add(one),
(HexColor::WHITE, false),
);
assert_eq!(
HexColor::WHITE.overflowing_add(one),
(HexColor::BLACK, true),
);
sourcepub const fn saturating_add(self, rhs: HexColor) -> HexColor
pub const fn saturating_add(self, rhs: HexColor) -> HexColor
Saturating color addition. Computes self + rhs
, saturating at the
numeric bounds instead of overflowing.
Examples
use hex_color::HexColor;
// Even though the green component should exceed 255, it saturates at
// 255 instead:
assert_eq!(
HexColor::YELLOW.saturating_add(HexColor::CYAN),
HexColor::WHITE,
);
sourcepub const fn wrapping_add(self, rhs: HexColor) -> HexColor
pub const fn wrapping_add(self, rhs: HexColor) -> HexColor
Wrapping (modular) addition. Computes self + rhs
, wrapping around the
boundary of u8
.
Examples
use hex_color::HexColor;
let almost_white = HexColor::achromatic(254);
let one = HexColor::achromatic(1);
assert_eq!(almost_white.wrapping_add(one), HexColor::WHITE);
assert_eq!(HexColor::WHITE.wrapping_add(one), HexColor::BLACK);
sourcepub const fn sub(self, rhs: HexColor) -> HexColor
pub const fn sub(self, rhs: HexColor) -> HexColor
Subtracts one color from another.
Each component is subtracted separately. The alpha component is ignored
entirely, always returning an RGB color (where alpha is the default
u8::MAX
).
Panics
Panics if any overflow occurs.
Examples
use hex_color::HexColor;
assert_eq!(HexColor::MAGENTA.sub(HexColor::BLUE), HexColor::RED);
assert_eq!(HexColor::YELLOW.sub(HexColor::RED), HexColor::GREEN);
assert_eq!(HexColor::CYAN.sub(HexColor::GREEN), HexColor::BLUE);
sourcepub const fn sub_scalar(self, n: u8) -> HexColor
pub const fn sub_scalar(self, n: u8) -> HexColor
sourcepub const fn checked_sub(self, rhs: HexColor) -> Option<HexColor>
pub const fn checked_sub(self, rhs: HexColor) -> Option<HexColor>
Checked color subtraction. Computes self - rhs
, returning None
if
overflow occurred.
Examples
use hex_color::HexColor;
let almost_black = HexColor::achromatic(1);
let one = HexColor::achromatic(1);
assert_eq!(almost_black.checked_sub(one), Some(HexColor::BLACK));
assert_eq!(HexColor::BLACK.checked_sub(one), None);
sourcepub const fn overflowing_sub(self, rhs: HexColor) -> (HexColor, bool)
pub const fn overflowing_sub(self, rhs: HexColor) -> (HexColor, bool)
Calculates self - rhs
.
Returns a tuple of the subtraction along with a boolean indicating whether any arithmetic overflow would occur. If an overflow would have occurred, then the wrapped value is returned.
Examples
use hex_color::HexColor;
let almost_black = HexColor::achromatic(1);
let one = HexColor::achromatic(1);
assert_eq!(
almost_black.overflowing_sub(one),
(HexColor::BLACK, false),
);
assert_eq!(
HexColor::BLACK.overflowing_sub(one),
(HexColor::WHITE, true),
);
sourcepub const fn saturating_sub(self, rhs: HexColor) -> HexColor
pub const fn saturating_sub(self, rhs: HexColor) -> HexColor
Saturating color subtraction. Computes self - rhs
, saturating at the
numeric bounds instead of overflowing.
Examples
use hex_color::HexColor;
// Even though the red component should overflow, it saturates at 0
// instead:
assert_eq!(
HexColor::CYAN.saturating_sub(HexColor::YELLOW),
HexColor::BLUE,
);
sourcepub const fn wrapping_sub(self, rhs: HexColor) -> HexColor
pub const fn wrapping_sub(self, rhs: HexColor) -> HexColor
Wrapping (modular) subtraction. Computes self - rhs
, wrapping around
the boundary of u8
.
Examples
use hex_color::HexColor;
let almost_black = HexColor::achromatic(1);
let one = HexColor::achromatic(1);
assert_eq!(almost_black.wrapping_sub(one), HexColor::BLACK);
assert_eq!(HexColor::BLACK.wrapping_sub(one), HexColor::WHITE);
sourcepub const fn mul(self, rhs: HexColor) -> HexColor
pub const fn mul(self, rhs: HexColor) -> HexColor
Multiplies two colors together.
Each component is multiplied separately. The alpha component is ignored
entirely, always returning an RGB color (where alpha is the default
u8::MAX
).
Panics
Panics if any overflow occurs.
Examples
use hex_color::HexColor;
let a = HexColor::rgb(1, 2, 3);
let b = HexColor::rgb(4, 5, 6);
assert_eq!(a.mul(b), HexColor::rgb(4, 10, 18));
sourcepub const fn checked_mul(self, rhs: HexColor) -> Option<HexColor>
pub const fn checked_mul(self, rhs: HexColor) -> Option<HexColor>
Checked color multiplication. Computes self * rhs
, returning None
if overflow occurred.
Examples
use hex_color::HexColor;
assert_eq!(
HexColor::achromatic(5).checked_mul(HexColor::achromatic(2)),
Some(HexColor::achromatic(10)),
);
assert_eq!(
HexColor::MAX.checked_mul(HexColor::achromatic(2)),
None,
);
sourcepub const fn overflowing_mul(self, rhs: HexColor) -> (HexColor, bool)
pub const fn overflowing_mul(self, rhs: HexColor) -> (HexColor, bool)
Calculates self * rhs
.
Returns a tuple of the multiplication along with a boolean indicating whether any arithmetic overflow would occur. If an overflow would have occurred, then the wrapped value is returned.
Examples
use hex_color::HexColor;
assert_eq!(
HexColor::achromatic(5).overflowing_mul(HexColor::achromatic(2)),
(HexColor::achromatic(10), false),
);
assert_eq!(
HexColor::achromatic(200).overflowing_mul(HexColor::achromatic(2)),
(HexColor::achromatic(144), true),
);
sourcepub const fn saturating_mul(self, rhs: HexColor) -> HexColor
pub const fn saturating_mul(self, rhs: HexColor) -> HexColor
Saturating color multiplication. Computes self * rhs
, saturating at
the numeric bounds instead of overflowing.
Examples
use hex_color::HexColor;
assert_eq!(
HexColor::achromatic(5).saturating_mul(HexColor::achromatic(2)),
HexColor::achromatic(10),
);
assert_eq!(
HexColor::achromatic(200).saturating_mul(HexColor::achromatic(2)),
HexColor::achromatic(255),
);
sourcepub const fn wrapping_mul(self, rhs: HexColor) -> HexColor
pub const fn wrapping_mul(self, rhs: HexColor) -> HexColor
Wrapping (modular) multiplication. Computes self * rhs
, wrapping
around at the boundary of the type.
Examples
use hex_color::HexColor;
assert_eq!(
HexColor::achromatic(5).wrapping_mul(HexColor::achromatic(2)),
HexColor::achromatic(10),
);
assert_eq!(
HexColor::achromatic(200).wrapping_mul(HexColor::achromatic(2)),
HexColor::achromatic(144),
);
sourcepub const fn div(self, rhs: HexColor) -> HexColor
pub const fn div(self, rhs: HexColor) -> HexColor
Divides one color with another.
Each component is divided separately. The alpha component is ignored
entirely, always returning an RGB color (where alpha is the default
u8::MAX
).
Panics
Panics if any component is divided by zero.
Examples
use hex_color::HexColor;
let a = HexColor::rgb(128, 64, 32);
let b = HexColor::rgb(2, 4, 8);
assert_eq!(a.div(b), HexColor::rgb(64, 16, 4));
sourcepub const fn checked_div(self, rhs: HexColor) -> Option<HexColor>
pub const fn checked_div(self, rhs: HexColor) -> Option<HexColor>
Checked color division. Computes self / rhs
, returning None
if
any component of rhs
is zero.
Examples
use hex_color::HexColor;
assert_eq!(
HexColor::achromatic(128).checked_div(HexColor::achromatic(2)),
Some(HexColor::achromatic(64)),
);
assert_eq!(
HexColor::WHITE.checked_div(HexColor::achromatic(0)),
None,
);
Trait Implementations
sourceimpl AddAssign<HexColor> for HexColor
impl AddAssign<HexColor> for HexColor
sourcefn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the +=
operation. Read more
sourceimpl<'de> Deserialize<'de> for HexColor
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for HexColor
serde
only.sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserializes a HexColor
from a string using the same rules as
HexColor::parse
.
To strictly deserialize either an RGB or RGBA value from a string, use
rgb::deserialize
or rgba::deserialize
respectively.
Examples
use hex_color::HexColor;
use serde::Deserialize;
use serde_json::json;
#[derive(Debug, PartialEq, Deserialize)]
struct Color {
name: String,
value: HexColor,
}
let saddle_brown = json!({
"name": "Saddle Brown",
"value": "#8B4513",
});
assert_eq!(
serde_json::from_value::<Color>(saddle_brown)?,
Color {
name: String::from("Saddle Brown"),
value: HexColor::rgb(139, 69, 19),
},
);
let transparent_cyan = json!({
"name": "Transparent Cyan",
"value": "#00FFFF80",
});
assert_eq!(
serde_json::from_value::<Color>(transparent_cyan)?,
Color {
name: String::from("Transparent Cyan"),
value: HexColor::rgba(0, 255, 255, 128),
},
);
sourceimpl Display for HexColor
impl Display for HexColor
sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Display the HexColor
as a hexadecimal value.
Examples
use hex_color::HexColor;
let rgba = HexColor::rgba(16, 32, 48, 64);
// By default it displays only a typical uppercase hexadecimal triplet
// even if the alpha is not fully opaque:
assert_eq!(format!("{rgba}"), "#102030");
// To display the alpha component, too, use the alternate flag:
assert_eq!(format!("{rgba:#}"), "#10203040");
sourceimpl Distribution<HexColor> for Standard
Available on crate feature rand
only.
impl Distribution<HexColor> for Standard
rand
only.sourcefn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> HexColor
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> HexColor
Generate a random value of T
, using rng
as the source of randomness.
sourcefn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>where
R: Rng,
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>where
R: Rng,
Create an iterator that generates random values of T
, using rng
as
the source of randomness. Read more
sourceimpl DivAssign<HexColor> for HexColor
impl DivAssign<HexColor> for HexColor
sourcefn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the /=
operation. Read more
sourceimpl From<HexColor> for u32
impl From<HexColor> for u32
sourcefn from(hex_color: HexColor) -> Self
fn from(hex_color: HexColor) -> Self
Constructs a new u32
from a HexColor
via HexColor::to_u32
.
sourceimpl From<u32> for HexColor
impl From<u32> for HexColor
sourcefn from(n: u32) -> Self
fn from(n: u32) -> Self
Constructs a new HexColor
from a u32
via HexColor::from_u32
.
sourceimpl FromStr for HexColor
impl FromStr for HexColor
sourcefn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Semantically identical to HexColor::parse
. For more information,
refer to that function’s documentation.
type Err = ParseHexColorError
type Err = ParseHexColorError
The associated error which can be returned from parsing.
sourceimpl MulAssign<HexColor> for HexColor
impl MulAssign<HexColor> for HexColor
sourcefn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the *=
operation. Read more
sourceimpl Ord for HexColor
impl Ord for HexColor
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl PartialEq<HexColor> for HexColor
impl PartialEq<HexColor> for HexColor
sourceimpl PartialOrd<HexColor> for HexColor
impl PartialOrd<HexColor> for HexColor
sourcefn partial_cmp(&self, other: &HexColor) -> Option<Ordering>
fn partial_cmp(&self, other: &HexColor) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl Serialize for HexColor
Available on crate feature serde
only.
impl Serialize for HexColor
serde
only.sourcefn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serializes the HexColor
as a string.
By default, HexColor
values get serialized in the form of #RRGGBB
strings when the alpha value is set to u8::MAX
, or completely
opaque. However, if any transparency exists, they are serialized in
the form of #RRGGBBAA
strings.
To strictly enforce getting serialized as an RGB or RGBA string, use
rgb::serialize
or rgba::serialize
respectively. In fact, using
either of these options is highly suggested for more normalized results.
Examples
use hex_color::HexColor;
use serde::Serialize;
use serde_json::json;
#[derive(Serialize)]
struct Color {
name: &'static str,
value: HexColor,
}
let orange = Color {
name: "Orange",
value: HexColor::rgb(255, 165, 0),
};
assert_eq!(
serde_json::to_value(orange)?,
json!({
"name": "Orange",
"value": "#FFA500",
}),
);
let transparent_navy = Color {
name: "Transparent Navy",
value: HexColor::rgba(0, 0, 128, 128),
};
assert_eq!(
serde_json::to_value(transparent_navy)?,
json!({
"name": "Transparent Navy",
"value": "#00008080",
}),
);
sourceimpl SubAssign<HexColor> for HexColor
impl SubAssign<HexColor> for HexColor
sourcefn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the -=
operation. Read more
impl Copy for HexColor
impl Eq for HexColor
impl StructuralEq for HexColor
impl StructuralPartialEq for HexColor
Auto Trait Implementations
impl RefUnwindSafe for HexColor
impl Send for HexColor
impl Sync for HexColor
impl Unpin for HexColor
impl UnwindSafe for HexColor
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more