pub struct Color {
pub a: f64,
pub r: f64,
pub g: f64,
pub b: f64,
pub color_space: ColorSpace,
}Expand description
An immutable color value in ARGB format.
Consider the light teal of the Flutter logo. It
is fully opaque, with a red r channel value of 0.2588 (or
0x42 or 66 as an 8-bit value), a green g channel value of
0.6471 (or 0xA5 or 165 as an 8-bit value), and a blue b
channel value of 0.9608 (or 0xF5 or 245 as an 8-bit value). In a common
CSS hex color syntax
for RGB color values, it would be described as #42A5F5.
Here are some ways it could be constructed:
let c1 = Color::from(1.0, 0.2588, 0.6471, 0.9608, ColorSpace::Srgb);
let c2 = Color::new(0xFF42A5F5);
let c3 = Color::from_argb(0xFF, 0x42, 0xA5, 0xF5);
let c4 = Color::from_argb(255, 66, 165, 245);
let c5 = Color::from_rgbo(66, 165, 245, 1.0);If you are having a problem with Color::new wherein it seems your color
is just not painting, check to make sure you are specifying the full 8
hexadecimal digits. If you only specify six, then the leading two digits are
assumed to be zero, which means fully-transparent:
let c1 = Color::new(0xFFFFFF); // fully transparent white (invisible)
let c2 = Color::new(0xFFFFFFFF); // fully opaque white (visible)
// Or use double-based channel values:
let c3 = Color::from(1.0, 1.0, 1.0, 1.0, ColorSpace::Srgb);Color’s color components are stored as floating-point values. Care should
be taken if one does not want the literal equality provided by PartialEq.
To test equality inside of Flutter tests consider using isSameColorAs.
See also:
- Colors, which defines the colors found in the Material Design specification.
isSameColorAs, a Matcher to handle floating-point deltas when checkingColorequality.
Fields§
§a: f64The alpha channel of this color.
r: f64The red channel of this color.
g: f64The green channel of this color.
b: f64The blue channel of this color.
color_space: ColorSpaceThe color space of this color.
Implementations§
Source§impl Color
impl Color
Sourcepub const fn new(value: u32) -> Color
pub const fn new(value: u32) -> Color
Construct a ColorSpace::Srgb color from the lower 32 bits of an integer.
The bits are interpreted as follows:
- Bits 24-31 are the alpha value.
- Bits 16-23 are the red value.
- Bits 8-15 are the green value.
- Bits 0-7 are the blue value.
In other words, if AA is the alpha value in hex, RR the red value in hex,
GG the green value in hex, and BB the blue value in hex, a color can be
expressed as Color::new(0xAARRGGBB).
For example, to get a fully opaque orange, you would use
Color::new(0xFFFF9000) (FF for the alpha, FF for the red, 90 for
the green, and 00 for the blue).
Note Each color is stored as floating-point color components, where the final value of each component is approximated by storing
c / 255, wherecis one of the four components (alpha, red, green, blue).
Sourcepub const fn from(
alpha: f64,
red: f64,
green: f64,
blue: f64,
color_space: ColorSpace,
) -> Color
pub const fn from( alpha: f64, red: f64, green: f64, blue: f64, color_space: ColorSpace, ) -> Color
Construct a color with floating-point color components.
Color components allows arbitrary bit depths for color components to be
supported. The values are interpreted relative to the ColorSpace
argument.
§Example
// Fully opaque maximum red color
let c1 = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::Srgb);
// Partially transparent moderately blue and green color
let c2 = Color::from(0.5, 0.0, 0.5, 0.5, ColorSpace::Srgb);
// Fully transparent color
let c3 = Color::from(0.0, 0.0, 0.0, 0.0, ColorSpace::Srgb);Sourcepub const fn from_argb(a: i32, r: i32, g: i32, b: i32) -> Color
pub const fn from_argb(a: i32, r: i32, g: i32, b: i32) -> Color
Construct an sRGB color from the lower 8 bits of four integers.
ais the alpha value, with 0 being transparent and 255 being fully opaque.ris red, from 0 to 255.gis green, from 0 to 255.bis blue, from 0 to 255.
Out of range values are brought into range using modulo 255.
See also from_rgbo, which takes the alpha value as
a floating point value.
Note Each color is stored as floating-point color components, where the final value of each component is approximated by storing
c / 255, wherecis one of the four components (alpha, red, green, blue).
Sourcepub const fn from_rgbo(r: i32, g: i32, b: i32, opacity: f64) -> Color
pub const fn from_rgbo(r: i32, g: i32, b: i32, opacity: f64) -> Color
Create an sRGB color from red, green, blue, and opacity, similar to
rgba() in CSS.
ris red, from 0 to 255.gis green, from 0 to 255.bis blue, from 0 to 255.opacityis alpha channel of this color as a double, with 0.0 being transparent and 1.0 being fully opaque.
Out of range values are brought into range using modulo 255.
See also from_argb, which takes the opacity as an
integer value.
Note Each color is stored as floating-point color components, where the final value of each component is approximated by storing
c / 255, wherecis one of the four components (alpha, red, green, blue).
Sourcepub fn value(&self) -> u32
👎Deprecated: Use component accessors like .r or .g, or to_argb32 for an explicit conversion
pub fn value(&self) -> u32
Use component accessors like .r or .g, or to_argb32 for an explicit conversion
A 32 bit value representing this color.
This getter is a stub. It is recommended instead to use the explicit
to_argb32 method.
Sourcepub fn to_argb32(&self) -> u32
pub fn to_argb32(&self) -> u32
Returns a 32-bit value representing this color.
The returned value is compatible with the default constructor
(Color::new) but does not guarantee to result in the same color due
to imprecisions in numeric conversions.
Unlike accessing the floating point equivalent channels individually
(a, r, g, b),
this method is intentionally lossy, and scales each channel using
(channel * 255.0).round().clamp(0, 255).
While useful for storing a 32-bit integer value, prefer accessing the individual channels (and storing the double equivalent) where higher precision is required.
The bits are assigned as follows:
- Bits 24-31 represents the
achannel as an 8-bit unsigned integer. - Bits 16-23 represents the
rchannel as an 8-bit unsigned integer. - Bits 8-15 represents the
gchannel as an 8-bit unsigned integer. - Bits 0-7 represents the
bchannel as an 8-bit unsigned integer.
Warning The value returned by this getter implicitly converts floating-point component values (such as
0.5) into their 8-bit equivalent by using theto_argb32method; the returned value is not guaranteed to be stable across different platforms or executions due to the complexity of floating-point math.
Sourcepub fn alpha(&self) -> i32
👎Deprecated: Use (*.a * 255.0).round().clamp(0, 255)
pub fn alpha(&self) -> i32
Use (*.a * 255.0).round().clamp(0, 255)
The alpha channel of this color in an 8 bit value.
A value of 0 means this color is fully transparent. A value of 255 means this color is fully opaque.
Sourcepub fn opacity(&self) -> f64
👎Deprecated: Use .a.
pub fn opacity(&self) -> f64
Use .a.
The alpha channel of this color as a double.
A value of 0.0 means this color is fully transparent. A value of 1.0 means this color is fully opaque.
Sourcepub fn red(&self) -> i32
👎Deprecated: Use (*.r * 255.0).round().clamp(0, 255)
pub fn red(&self) -> i32
Use (*.r * 255.0).round().clamp(0, 255)
The red channel of this color in an 8 bit value.
Sourcepub fn green(&self) -> i32
👎Deprecated: Use (*.g * 255.0).round().clamp(0, 255)
pub fn green(&self) -> i32
Use (*.g * 255.0).round().clamp(0, 255)
The green channel of this color in an 8 bit value.
Sourcepub fn blue(&self) -> i32
👎Deprecated: Use (*.b * 255.0).round().clamp(0, 255)
pub fn blue(&self) -> i32
Use (*.b * 255.0).round().clamp(0, 255)
The blue channel of this color in an 8 bit value.
Sourcepub fn with_values(
&self,
alpha: Option<f64>,
red: Option<f64>,
green: Option<f64>,
blue: Option<f64>,
color_space: Option<ColorSpace>,
) -> Color
pub fn with_values( &self, alpha: Option<f64>, red: Option<f64>, green: Option<f64>, blue: Option<f64>, color_space: Option<ColorSpace>, ) -> Color
Returns a new color with the provided components updated.
Each component (alpha, red, green, blue) represents a
floating-point value; see Color::from for details and examples.
If color_space is provided, and is different than the current color
space, the component values are updated before transforming them to the
provided ColorSpace.
Example:
/// Create a color with 50% opacity.
fn make_transparent(color: Color) -> Color {
color.with_values(Some(0.5), None, None, None, None)
}Sourcepub fn with_alpha(&self, a: i32) -> Color
pub fn with_alpha(&self, a: i32) -> Color
Returns a new color that matches this color with the alpha channel
replaced with a (which ranges from 0 to 255).
Out of range values will have unexpected effects.
Sourcepub fn with_opacity(&self, opacity: f64) -> Color
👎Deprecated: Use .with_values() to avoid precision loss.
pub fn with_opacity(&self, opacity: f64) -> Color
Use .with_values() to avoid precision loss.
Returns a new color that matches this color with the alpha channel
replaced with the given opacity (which ranges from 0.0 to 1.0).
Out of range values will have unexpected effects.
Sourcepub fn with_red(&self, r: i32) -> Color
pub fn with_red(&self, r: i32) -> Color
Returns a new color that matches this color with the red channel replaced
with r (which ranges from 0 to 255).
Out of range values will have unexpected effects.
Sourcepub fn with_green(&self, g: i32) -> Color
pub fn with_green(&self, g: i32) -> Color
Returns a new color that matches this color with the green channel
replaced with g (which ranges from 0 to 255).
Out of range values will have unexpected effects.
Sourcepub fn with_blue(&self, b: i32) -> Color
pub fn with_blue(&self, b: i32) -> Color
Returns a new color that matches this color with the blue channel replaced
with b (which ranges from 0 to 255).
Out of range values will have unexpected effects.
Sourcepub fn compute_luminance(&self) -> f64
pub fn compute_luminance(&self) -> f64
Returns a brightness value between 0 for darkest and 1 for lightest.
Represents the relative luminance of the color. This value is computationally expensive to calculate.
Sourcepub fn lerp(x: Option<Color>, y: Option<Color>, t: f64) -> Option<Color>
pub fn lerp(x: Option<Color>, y: Option<Color>, t: f64) -> Option<Color>
Linearly interpolate between two colors.
This is intended to be fast but as a result may be ugly. Consider
HSVColor or writing custom logic for interpolating colors.
If either color is null, this function linearly interpolates from a
transparent instance of the other color. This is usually preferable to
interpolating from material.Colors.transparent (Color::new(0x00000000)),
which is specifically transparent black.
The t argument represents position on the timeline, with 0.0 meaning
that the interpolation has not started, returning a (or something
equivalent to a), 1.0 meaning that the interpolation has finished,
returning b (or something equivalent to b), and values in between
meaning that the interpolation is at the relevant point on the timeline
between a and b. The interpolation can be extrapolated beyond 0.0 and
1.0, so negative values and values greater than 1.0 are valid (and can
easily be generated by curves such as Curves.elasticInOut). Each channel
will be clamped to the range 0 to 255.
Values for t are usually obtained from an Animation<f64>, such as
an AnimationController.
If the two colors are in different color spaces, both are converted to the wider gamut color space before interpolating. The result will be in the wider gamut color space. For example, interpolating between an sRGB color and a Display P3 color will produce a Display P3 result.
Sourcepub fn alpha_blend(foreground: Color, background: Color) -> Color
pub fn alpha_blend(foreground: Color, background: Color) -> Color
Combine the foreground color as a transparent color over top of a background color, and return the resulting combined color.
This uses standard alpha blending (“SRC over DST”) rules to produce a blended color from two colors. This can be used as a performance enhancement when trying to avoid needless alpha blending compositing operations for two things that are solid colors with the same shape, but overlay each other: instead, just paint one with the combined color.
Sourcepub fn get_alpha_from_opacity(opacity: f64) -> i32
pub fn get_alpha_from_opacity(opacity: f64) -> i32
Returns an alpha value representative of the provided opacity value.
The opacity value may not be null.