Color class - dart:ui library (original) (raw)
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
or165
as an 8-bit value), and a blue b channel value of 0.9608
(or0xF5
or 245
as an 8-bit value). In a common CSS hex color syntaxfor RGB color values, it would be described as #42A5F5
.
Here are some ways it could be constructed:
const Color c1 = Color.from(alpha: 1.0, red: 0.2588, green: 0.6471, blue: 0.9608);
const Color c2 = Color(0xFF42A5F5);
const Color c3 = Color.fromARGB(0xFF, 0x42, 0xA5, 0xF5);
const Color c4 = Color.fromARGB(255, 66, 165, 245);
const Color c5 = Color.fromRGBO(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:
const Color c1 = Color(0xFFFFFF); // fully transparent white (invisible)
const Color c2 = Color(0xFFFFFFFF); // fully opaque white (visible)
// Or use double-based channel values:
const Color c3 = Color.from(alpha: 1.0, red: 1.0, green: 1.0, blue: 1.0);
Color's color components are stored as floating-point values. Care should be taken if one does not want the literal equality provided by operator==
. 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 checking Color equality.
Implementers
Constructors
Construct an ColorSpace.sRGB color from the lower 32 bits of an int.
const
Color.from({required double alpha, required double red, required double green, required double blue, ColorSpace colorSpace = ColorSpace.sRGB})
Construct a color with floating-point color components.
const
Color.fromARGB(int a, int r, int g, int b)
Construct an sRGB color from the lower 8 bits of four integers.
const
Color.fromRGBO(int r, int g, int b, double opacity)
Create an sRGB color from red, green, blue, and opacity, similar torgba()
in CSS.
const
Properties
The alpha channel of this color.
final
The alpha channel of this color in an 8 bit value.
no setter
The blue channel of this color.
final
The blue channel of this color in an 8 bit value.
no setter
The color space of this color.
final
The green channel of this color.
final
The green channel of this color in an 8 bit value.
no setter
The hash code for this object.
no setteroverride
The alpha channel of this color as a double.
no setter
The red channel of this color.
final
The red channel of this color in an 8 bit value.
no setter
A representation of the runtime type of the object.
no setterinherited
A 32 bit value representing this color.
no setter
Methods
Returns a brightness value between 0 for darkest and 1 for lightest.
noSuchMethod(Invocation invocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
Returns a 32-bit value representing this color.
A string representation of this object.
override
Returns a new color that matches this color with the alpha channel replaced with a
(which ranges from 0 to 255).
Returns a new color that matches this color with the blue channel replaced with b
(which ranges from 0 to 255).
Returns a new color that matches this color with the green channel replaced with g
(which ranges from 0 to 255).
withOpacity(double opacity)→ Color
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).
Returns a new color that matches this color with the red channel replaced with r
(which ranges from 0 to 255).
withValues({double? alpha, double? red, double? green, double? blue, ColorSpace? colorSpace})→ Color
Returns a new color with the provided components updated.
Operators
operator ==(Object other)→ bool
The equality operator.
override
Static Methods
alphaBlend(Color foreground, Color background)→ Color
Combine the foreground color as a transparent color over top of a background color, and return the resulting combined color.
getAlphaFromOpacity(double opacity)→ int
Returns an alpha value representative of the provided opacity
value.
lerp(Color? x, Color? y, double t)→ Color?
Linearly interpolate between two colors.