Skip to content

Commit facf930

Browse files
committed
A Color.fromRGBO
Sometimes it is convenient to construct a color from an opacity rather than an alpha value. Fixes flutter/flutter#5205
1 parent 6c5315c commit facf930

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎lib/ui/painting.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,40 @@ class Color {
1919
const Color(int value) : value = value & 0xFFFFFFFF;
2020

2121
/// Construct a color from the lower 8 bits of four integers.
22+
///
23+
/// * `a` is the alpha value, with 0 being transparent and 255 being fully
24+
/// opaque.
25+
/// * `r` is [red], from 0 to 255.
26+
/// * `g` is [red], from 0 to 255.
27+
/// * `b` is [red], from 0 to 255.
28+
///
29+
/// Out of range values are brought into range using modulo 255.
30+
///
31+
/// See also [fromARGB], which takes the alpha value as a floating point
32+
/// value.
2233
const Color.fromARGB(int a, int r, int g, int b) :
2334
value = ((((a & 0xff) << 24) |
2435
((r & 0xff) << 16) |
2536
((g & 0xff) << 8) |
2637
((b & 0xff) << 0)) & 0xFFFFFFFF);
2738

39+
/// Create a color from red, green, blue, and opacity, similar to `rgba()` in CSS.
40+
///
41+
/// * `r` is [red], from 0 to 255.
42+
/// * `g` is [red], from 0 to 255.
43+
/// * `b` is [red], from 0 to 255.
44+
/// * `opacity` is alpha channel of this color as a double, with 0.0 being
45+
/// transparent and 1.0 being fully opaque.
46+
///
47+
/// Out of range values are brought into range using modulo 255.
48+
///
49+
/// See also [fromARGB], which takes the opacity as an integer value.
50+
const Color.fromRGBO(int r, int g, int b, double opacity) :
51+
value = (((((opacity * 0xff ~/ 1) & 0xff) << 24) |
52+
((r & 0xff) << 16) |
53+
((g & 0xff) << 8) |
54+
((b & 0xff) << 0)) & 0xFFFFFFFF);
55+
2856
/// A 32 bit value representing this color.
2957
///
3058
/// Bits 24-31 are the alpha value.
@@ -34,9 +62,15 @@ class Color {
3462
final int value;
3563

3664
/// The alpha channel of this color in an 8 bit value.
65+
///
66+
/// A value of 0 means this color is fully transparent. A value of 255 means
67+
/// this color is fully opaque.
3768
int get alpha => (0xff000000 & value) >> 24;
3869

3970
/// The alpha channel of this color as a double.
71+
///
72+
/// A value of 0.0 means this color is fully transparent. A value of 1.0 means
73+
/// this color is fully opaque.
4074
double get opacity => alpha / 0xFF;
4175

4276
/// The red channel of this color in an 8 bit value.

0 commit comments

Comments
 (0)