Removes the int storage from Color (#54714)
issue: https://github.com/flutter/flutter/issues/127855
This depends on https://github.com/flutter/flutter/pull/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
diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart
index 1a67e9a..202f499 100644
--- a/lib/ui/painting.dart
+++ b/lib/ui/painting.dart
@@ -100,12 +100,8 @@
/// Color(0xFFFF9000)` (`FF` for the alpha, `FF` for the red, `90` for the
/// green, and `00` for the blue).
const Color(int value)
- : _value = value & 0xFFFFFFFF,
- colorSpace = ColorSpace.sRGB,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ : this._fromARGBC(
+ value >> 24, value >> 16, value >> 8, value, ColorSpace.sRGB);
/// Construct a color with normalized color components.
///
@@ -118,11 +114,10 @@
required double green,
required double blue,
this.colorSpace = ColorSpace.sRGB})
- : _value = 0,
- _a = alpha,
- _r = red,
- _g = green,
- _b = blue;
+ : a = alpha,
+ r = red,
+ g = green,
+ b = blue;
/// Construct an sRGB color from the lower 8 bits of four integers.
///
@@ -137,28 +132,12 @@
/// See also [fromRGBO], which takes the alpha value as a floating point
/// value.
const Color.fromARGB(int a, int r, int g, int b)
- : _value = (((a & 0xff) << 24) |
- ((r & 0xff) << 16) |
- ((g & 0xff) << 8) |
- ((b & 0xff) << 0)) &
- 0xFFFFFFFF,
- colorSpace = ColorSpace.sRGB,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ : this._fromARGBC(a, r, g, b, ColorSpace.sRGB);
const Color._fromARGBC(
- int alpha, int red, int green, int blue, this.colorSpace)
- : _value = (((alpha & 0xff) << 24) |
- ((red & 0xff) << 16) |
- ((green & 0xff) << 8) |
- ((blue & 0xff) << 0)) &
- 0xFFFFFFFF,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ int alpha, int red, int green, int blue, ColorSpace colorSpace)
+ : this._fromRGBOC(
+ red, green, blue, (alpha & 0xff) / 255, colorSpace);
/// Create an sRGB color from red, green, blue, and opacity, similar to
/// `rgba()` in CSS.
@@ -173,41 +152,34 @@
///
/// See also [fromARGB], which takes the opacity as an integer value.
const Color.fromRGBO(int r, int g, int b, double opacity)
- : _value = ((((opacity * 0xff ~/ 1) & 0xff) << 24) |
- ((r & 0xff) << 16) |
- ((g & 0xff) << 8) |
- ((b & 0xff) << 0)) &
- 0xFFFFFFFF,
- colorSpace = ColorSpace.sRGB,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ : this._fromRGBOC(r, g, b, opacity, ColorSpace.sRGB);
+
+ const Color._fromRGBOC(int r, int g, int b, double opacity, this.colorSpace)
+ : a = opacity,
+ r = (r & 0xff) / 255,
+ g = (g & 0xff) / 255,
+ b = (b & 0xff) / 255;
/// The alpha channel of this color.
///
/// A value of 0.0 means this color is fully transparent. A value of 1.0 means
/// this color is fully opaque.
- double get a => _a ?? (alpha / 255);
- final double? _a;
+ final double a;
/// The red channel of this color.
- double get r => _r ?? (red / 255);
- final double? _r;
+ final double r;
/// The green channel of this color.
- double get g => _g ?? (green / 255);
- final double? _g;
+ final double g;
/// The blue channel of this color.
- double get b => _b ?? (blue / 255);
- final double? _b;
+ final double b;
/// The color space of this color.
final ColorSpace colorSpace;
static int _floatToInt8(double x) {
- return ((x * 255.0).round()) & 0xff;
+ return (x * 255.0).round() & 0xff;
}
/// A 32 bit value representing this color.
@@ -220,16 +192,11 @@
/// * Bits 0-7 are the blue value.
@Deprecated('Use component accessors like .r or .g.')
int get value {
- if (_a != null && _r != null && _g != null && _b != null) {
- return _floatToInt8(_a) << 24 |
- _floatToInt8(_r) << 16 |
- _floatToInt8(_g) << 8 |
- _floatToInt8(_b) << 0;
- } else {
- return _value;
- }
+ return _floatToInt8(a) << 24 |
+ _floatToInt8(r) << 16 |
+ _floatToInt8(g) << 8 |
+ _floatToInt8(b) << 0;
}
- final int _value;
/// The alpha channel of this color in an 8 bit value.
///
diff --git a/lib/web_ui/lib/painting.dart b/lib/web_ui/lib/painting.dart
index a9f7d9e..113caba 100644
--- a/lib/web_ui/lib/painting.dart
+++ b/lib/web_ui/lib/painting.dart
@@ -23,12 +23,8 @@
class Color {
const Color(int value)
- : _value = value & 0xFFFFFFFF,
- colorSpace = ColorSpace.sRGB,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ : this._fromARGBC(
+ value >> 24, value >> 16, value >> 8, value, ColorSpace.sRGB);
const Color.from(
{required double alpha,
@@ -36,77 +32,49 @@
required double green,
required double blue,
this.colorSpace = ColorSpace.sRGB})
- : _value = 0,
- _a = alpha,
- _r = red,
- _g = green,
- _b = blue;
+ : a = alpha,
+ r = red,
+ g = green,
+ b = blue;
const Color.fromARGB(int a, int r, int g, int b)
- : _value = (((a & 0xff) << 24) |
- ((r & 0xff) << 16) |
- ((g & 0xff) << 8) |
- ((b & 0xff) << 0)) &
- 0xFFFFFFFF,
- colorSpace = ColorSpace.sRGB,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ : this._fromARGBC(a, r, g, b, ColorSpace.sRGB);
const Color._fromARGBC(
- int alpha, int red, int green, int blue, this.colorSpace)
- : _value = (((alpha & 0xff) << 24) |
- ((red & 0xff) << 16) |
- ((green & 0xff) << 8) |
- ((blue & 0xff) << 0)) &
- 0xFFFFFFFF,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ int alpha, int red, int green, int blue, ColorSpace colorSpace)
+ : this._fromRGBOC(
+ red, green, blue, (alpha & 0xff) / 255, colorSpace);
const Color.fromRGBO(int r, int g, int b, double opacity)
- : _value = ((((opacity * 0xff ~/ 1) & 0xff) << 24) |
- ((r & 0xff) << 16) |
- ((g & 0xff) << 8) |
- ((b & 0xff) << 0)) &
- 0xFFFFFFFF,
- colorSpace = ColorSpace.sRGB,
- _a = null,
- _r = null,
- _g = null,
- _b = null;
+ : this._fromRGBOC(r, g, b, opacity, ColorSpace.sRGB);
- double get a => _a ?? (alpha / 255);
- final double? _a;
+ const Color._fromRGBOC(int r, int g, int b, double opacity, this.colorSpace)
+ : a = opacity,
+ r = (r & 0xff) / 255,
+ g = (g & 0xff) / 255,
+ b = (b & 0xff) / 255;
- double get r => _r ?? (red / 255);
- final double? _r;
+ final double a;
- double get g => _g ?? (green / 255);
- final double? _g;
+ final double r;
- double get b => _b ?? (blue / 255);
- final double? _b;
+ final double g;
+
+ final double b;
final ColorSpace colorSpace;
static int _floatToInt8(double x) {
- return ((x * 255.0).round()) & 0xff;
+ return (x * 255.0).round() & 0xff;
}
int get value {
- if (_a != null && _r != null && _g != null && _b != null) {
- return _floatToInt8(_a) << 24 |
- _floatToInt8(_r) << 16 |
- _floatToInt8(_g) << 8 |
- _floatToInt8(_b) << 0;
- } else {
- return _value;
- }
+ return _floatToInt8(a) << 24 |
+ _floatToInt8(r) << 16 |
+ _floatToInt8(g) << 8 |
+ _floatToInt8(b) << 0;
}
- final int _value;
+
int get alpha => (0xff000000 & value) >> 24;