Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1cd3306

Browse files
authored
Removes the int storage from Color (#54714)
issue: flutter/flutter#127855 This depends on flutter/flutter#153938 being landed. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
1 parent b51276f commit 1cd3306

File tree

2 files changed

+53
-118
lines changed

2 files changed

+53
-118
lines changed

lib/ui/painting.dart

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,8 @@ class Color {
100100
/// Color(0xFFFF9000)` (`FF` for the alpha, `FF` for the red, `90` for the
101101
/// green, and `00` for the blue).
102102
const Color(int value)
103-
: _value = value & 0xFFFFFFFF,
104-
colorSpace = ColorSpace.sRGB,
105-
_a = null,
106-
_r = null,
107-
_g = null,
108-
_b = null;
103+
: this._fromARGBC(
104+
value >> 24, value >> 16, value >> 8, value, ColorSpace.sRGB);
109105

110106
/// Construct a color with normalized color components.
111107
///
@@ -118,11 +114,10 @@ class Color {
118114
required double green,
119115
required double blue,
120116
this.colorSpace = ColorSpace.sRGB})
121-
: _value = 0,
122-
_a = alpha,
123-
_r = red,
124-
_g = green,
125-
_b = blue;
117+
: a = alpha,
118+
r = red,
119+
g = green,
120+
b = blue;
126121

127122
/// Construct an sRGB color from the lower 8 bits of four integers.
128123
///
@@ -137,28 +132,12 @@ class Color {
137132
/// See also [fromRGBO], which takes the alpha value as a floating point
138133
/// value.
139134
const Color.fromARGB(int a, int r, int g, int b)
140-
: _value = (((a & 0xff) << 24) |
141-
((r & 0xff) << 16) |
142-
((g & 0xff) << 8) |
143-
((b & 0xff) << 0)) &
144-
0xFFFFFFFF,
145-
colorSpace = ColorSpace.sRGB,
146-
_a = null,
147-
_r = null,
148-
_g = null,
149-
_b = null;
135+
: this._fromARGBC(a, r, g, b, ColorSpace.sRGB);
150136

151137
const Color._fromARGBC(
152-
int alpha, int red, int green, int blue, this.colorSpace)
153-
: _value = (((alpha & 0xff) << 24) |
154-
((red & 0xff) << 16) |
155-
((green & 0xff) << 8) |
156-
((blue & 0xff) << 0)) &
157-
0xFFFFFFFF,
158-
_a = null,
159-
_r = null,
160-
_g = null,
161-
_b = null;
138+
int alpha, int red, int green, int blue, ColorSpace colorSpace)
139+
: this._fromRGBOC(
140+
red, green, blue, (alpha & 0xff) / 255, colorSpace);
162141

163142
/// Create an sRGB color from red, green, blue, and opacity, similar to
164143
/// `rgba()` in CSS.
@@ -173,41 +152,34 @@ class Color {
173152
///
174153
/// See also [fromARGB], which takes the opacity as an integer value.
175154
const Color.fromRGBO(int r, int g, int b, double opacity)
176-
: _value = ((((opacity * 0xff ~/ 1) & 0xff) << 24) |
177-
((r & 0xff) << 16) |
178-
((g & 0xff) << 8) |
179-
((b & 0xff) << 0)) &
180-
0xFFFFFFFF,
181-
colorSpace = ColorSpace.sRGB,
182-
_a = null,
183-
_r = null,
184-
_g = null,
185-
_b = null;
155+
: this._fromRGBOC(r, g, b, opacity, ColorSpace.sRGB);
156+
157+
const Color._fromRGBOC(int r, int g, int b, double opacity, this.colorSpace)
158+
: a = opacity,
159+
r = (r & 0xff) / 255,
160+
g = (g & 0xff) / 255,
161+
b = (b & 0xff) / 255;
186162

187163
/// The alpha channel of this color.
188164
///
189165
/// A value of 0.0 means this color is fully transparent. A value of 1.0 means
190166
/// this color is fully opaque.
191-
double get a => _a ?? (alpha / 255);
192-
final double? _a;
167+
final double a;
193168

194169
/// The red channel of this color.
195-
double get r => _r ?? (red / 255);
196-
final double? _r;
170+
final double r;
197171

198172
/// The green channel of this color.
199-
double get g => _g ?? (green / 255);
200-
final double? _g;
173+
final double g;
201174

202175
/// The blue channel of this color.
203-
double get b => _b ?? (blue / 255);
204-
final double? _b;
176+
final double b;
205177

206178
/// The color space of this color.
207179
final ColorSpace colorSpace;
208180

209181
static int _floatToInt8(double x) {
210-
return ((x * 255.0).round()) & 0xff;
182+
return (x * 255.0).round() & 0xff;
211183
}
212184

213185
/// A 32 bit value representing this color.
@@ -220,16 +192,11 @@ class Color {
220192
/// * Bits 0-7 are the blue value.
221193
@Deprecated('Use component accessors like .r or .g.')
222194
int get value {
223-
if (_a != null && _r != null && _g != null && _b != null) {
224-
return _floatToInt8(_a) << 24 |
225-
_floatToInt8(_r) << 16 |
226-
_floatToInt8(_g) << 8 |
227-
_floatToInt8(_b) << 0;
228-
} else {
229-
return _value;
230-
}
195+
return _floatToInt8(a) << 24 |
196+
_floatToInt8(r) << 16 |
197+
_floatToInt8(g) << 8 |
198+
_floatToInt8(b) << 0;
231199
}
232-
final int _value;
233200

234201
/// The alpha channel of this color in an 8 bit value.
235202
///

lib/web_ui/lib/painting.dart

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,90 +23,58 @@ Color _scaleAlpha(Color a, double factor) {
2323

2424
class Color {
2525
const Color(int value)
26-
: _value = value & 0xFFFFFFFF,
27-
colorSpace = ColorSpace.sRGB,
28-
_a = null,
29-
_r = null,
30-
_g = null,
31-
_b = null;
26+
: this._fromARGBC(
27+
value >> 24, value >> 16, value >> 8, value, ColorSpace.sRGB);
3228

3329
const Color.from(
3430
{required double alpha,
3531
required double red,
3632
required double green,
3733
required double blue,
3834
this.colorSpace = ColorSpace.sRGB})
39-
: _value = 0,
40-
_a = alpha,
41-
_r = red,
42-
_g = green,
43-
_b = blue;
35+
: a = alpha,
36+
r = red,
37+
g = green,
38+
b = blue;
4439

4540
const Color.fromARGB(int a, int r, int g, int b)
46-
: _value = (((a & 0xff) << 24) |
47-
((r & 0xff) << 16) |
48-
((g & 0xff) << 8) |
49-
((b & 0xff) << 0)) &
50-
0xFFFFFFFF,
51-
colorSpace = ColorSpace.sRGB,
52-
_a = null,
53-
_r = null,
54-
_g = null,
55-
_b = null;
41+
: this._fromARGBC(a, r, g, b, ColorSpace.sRGB);
5642

5743
const Color._fromARGBC(
58-
int alpha, int red, int green, int blue, this.colorSpace)
59-
: _value = (((alpha & 0xff) << 24) |
60-
((red & 0xff) << 16) |
61-
((green & 0xff) << 8) |
62-
((blue & 0xff) << 0)) &
63-
0xFFFFFFFF,
64-
_a = null,
65-
_r = null,
66-
_g = null,
67-
_b = null;
44+
int alpha, int red, int green, int blue, ColorSpace colorSpace)
45+
: this._fromRGBOC(
46+
red, green, blue, (alpha & 0xff) / 255, colorSpace);
6847

6948
const Color.fromRGBO(int r, int g, int b, double opacity)
70-
: _value = ((((opacity * 0xff ~/ 1) & 0xff) << 24) |
71-
((r & 0xff) << 16) |
72-
((g & 0xff) << 8) |
73-
((b & 0xff) << 0)) &
74-
0xFFFFFFFF,
75-
colorSpace = ColorSpace.sRGB,
76-
_a = null,
77-
_r = null,
78-
_g = null,
79-
_b = null;
49+
: this._fromRGBOC(r, g, b, opacity, ColorSpace.sRGB);
8050

81-
double get a => _a ?? (alpha / 255);
82-
final double? _a;
51+
const Color._fromRGBOC(int r, int g, int b, double opacity, this.colorSpace)
52+
: a = opacity,
53+
r = (r & 0xff) / 255,
54+
g = (g & 0xff) / 255,
55+
b = (b & 0xff) / 255;
8356

84-
double get r => _r ?? (red / 255);
85-
final double? _r;
57+
final double a;
8658

87-
double get g => _g ?? (green / 255);
88-
final double? _g;
59+
final double r;
8960

90-
double get b => _b ?? (blue / 255);
91-
final double? _b;
61+
final double g;
62+
63+
final double b;
9264

9365
final ColorSpace colorSpace;
9466

9567
static int _floatToInt8(double x) {
96-
return ((x * 255.0).round()) & 0xff;
68+
return (x * 255.0).round() & 0xff;
9769
}
9870

9971
int get value {
100-
if (_a != null && _r != null && _g != null && _b != null) {
101-
return _floatToInt8(_a) << 24 |
102-
_floatToInt8(_r) << 16 |
103-
_floatToInt8(_g) << 8 |
104-
_floatToInt8(_b) << 0;
105-
} else {
106-
return _value;
107-
}
72+
return _floatToInt8(a) << 24 |
73+
_floatToInt8(r) << 16 |
74+
_floatToInt8(g) << 8 |
75+
_floatToInt8(b) << 0;
10876
}
109-
final int _value;
77+
11078

11179
int get alpha => (0xff000000 & value) >> 24;
11280

0 commit comments

Comments
 (0)