Last active
February 16, 2025 11:52
-
-
Save rydmike/8efe0acf5a24d7e4fd029530c46e7536 to your computer and use it in GitHub Desktop.
Legacy int API extensions on Color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui'; | |
/// Legacy int API extensions on [Color]. | |
/// | |
/// Convenience [Color] sRGB extensions that can be used as none deprecated | |
/// replacements for `alpha`, `red`, `green`, `blue` and `value` they are | |
/// called [alpha8bit], [red8bit], [green8bit], [blue8bit] and [value32bit]. | |
/// You can use them to avoid using the deprecated color properties. | |
extension LegacyIntColorExtensions on Color { | |
/// A 32 bit value representing this color. | |
/// | |
/// This feature brings back the Color.value API in a way that is not and | |
/// will not be deprecated. | |
/// | |
/// The bits are assigned 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. | |
int get value32bit { | |
return _floatToInt8(a) << 24 | | |
_floatToInt8(r) << 16 | | |
_floatToInt8(g) << 8 | | |
_floatToInt8(b) << 0; | |
} | |
/// 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. | |
/// | |
/// This feature brings back the Color.alpha API in a way that is not and | |
/// will not be deprecated. | |
int get alpha8bit => (0xff000000 & value32bit) >> 24; | |
/// The red channel of this color in an 8 bit value. | |
/// | |
/// This feature brings back the Color.red API in a way that is not and | |
/// will not be deprecated. | |
int get red8bit => (0x00ff0000 & value32bit) >> 16; | |
/// The green channel of this color in an 8 bit value. | |
/// | |
/// This feature brings back the Color.green API in a way that is not and | |
/// will not be deprecated. | |
int get green8bit => (0x0000ff00 & value32bit) >> 8; | |
/// The blue channel of this color in an 8 bit value. | |
/// | |
/// This feature brings back the Color.blue API in a way that is not and | |
/// will not be deprecated. | |
int get blue8bit => (0x000000ff & value32bit) >> 0; | |
// Convert float to 8 bit integer. | |
int _floatToInt8(double x) { | |
return (x * 255.0).round() & 0xff; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment