pub struct Color {
pub name: Option<String>,
/* private fields */
}
Expand description
Represents a single color in the palette.
The color components are stored in extended linear sRGB color space, but the API primarily works with non-linear (gamma-corrected) values which better match human perception.
Fields§
§name: Option<String>
Optional name for the color.
Implementations§
Source§impl Color
impl Color
Sourcepub fn new(
red: f32,
green: f32,
blue: f32,
opacity: Option<f32>,
name: Option<String>,
) -> Self
pub fn new( red: f32, green: f32, blue: f32, opacity: Option<f32>, name: Option<String>, ) -> Self
Creates a color using extended non-linear sRGB components.
-
Note: If you don’t know the difference between linear and non-linear, this is the initializer you want.
-
Parameters:
- red: Red component.
- green: Green component.
- blue: Blue component.
- opacity: Optional opacity value (0.0…1.0), defaults to 1.0.
- name: Optional name for the color.
Sourcepub fn from_linear(
red: f32,
green: f32,
blue: f32,
opacity: Option<f32>,
name: Option<String>,
) -> Self
pub fn from_linear( red: f32, green: f32, blue: f32, opacity: Option<f32>, name: Option<String>, ) -> Self
Creates a color using extended linear sRGB components.
-
Note: For most purposes, you will want to use
new()
instead. -
Parameters:
- red: Linear red component.
- green: Linear green component.
- blue: Linear blue component.
- opacity: Optional opacity value (0.0…1.0), defaults to 1.0.
- name: Optional name for the color.
Sourcepub fn components(&self) -> ColorComponents
pub fn components(&self) -> ColorComponents
Returns the color components in extended non-linear sRGB color space.
This is probably what you want. The color components are adjusted for human perception and display on screens.
Sourcepub fn components_linear(&self) -> &ColorComponents
pub fn components_linear(&self) -> &ColorComponents
Returns the color components in extended linear sRGB color space.
- Note: For most purposes, you will want to use
components()
instead which returns normal sRGB values.
Sourcepub fn from_hex_str(hex: &str) -> Result<Self, &'static str>
pub fn from_hex_str(hex: &str) -> Result<Self, &'static str>
Creates a color from a hex string.
Supports the following formats:
- RGB: “#RGB” or “RGB”
- RGBA: “#RGBA” or “RGBA”
- RRGGBB: “#RRGGBB” or “RRGGBB”
- RRGGBBAA: “#RRGGBBAA” or “RRGGBBAA”
The “#” prefix is optional.
Returns an error if the string format is invalid.
Sourcepub fn from_hex_int(hex: u32) -> Result<Self, &'static str>
pub fn from_hex_int(hex: u32) -> Result<Self, &'static str>
Creates a color from an integer hex value.
Supports the following formats:
- RGB: 0xRGB (12-bit)
- RGBA: 0xRGBA (16-bit)
- RRGGBB: 0xRRGGBB (24-bit)
- RRGGBBAA: 0xRRGGBBAA (32-bit)