|
| 1 | +import 'package:meta/meta.dart'; |
| 2 | + |
| 3 | +/// Basic data class holding a Color in ARGB format. |
| 4 | +/// This can be converted to dart:ui's Color using the flame_tiled package |
| 5 | +@immutable |
| 6 | +class ColorData { |
| 7 | + static int _sub(int hex, int index) => (hex >> index * 8) & 0x000000ff; |
| 8 | + |
| 9 | + final int _hex; |
| 10 | + |
| 11 | + int get alpha => _sub(_hex, 3); |
| 12 | + |
| 13 | + int get red => _sub(_hex, 2); |
| 14 | + |
| 15 | + int get green => _sub(_hex, 1); |
| 16 | + |
| 17 | + int get blue => _sub(_hex, 0); |
| 18 | + |
| 19 | + /// Parses the Color from an int using the lower 32-bits and tiled's format: |
| 20 | + /// 0xaarrggbb |
| 21 | + const ColorData.hex(this._hex); |
| 22 | + |
| 23 | + const ColorData.rgb(int red, int green, int blue, [int alpha = 255]) |
| 24 | + : assert(red >= 0 && red <= 255), |
| 25 | + assert(green >= 0 && green <= 255), |
| 26 | + assert(blue >= 0 && blue <= 255), |
| 27 | + assert(alpha >= 0 && alpha <= 255), |
| 28 | + _hex = (alpha << 3 * 8) + |
| 29 | + (red << 2 * 8) + |
| 30 | + (green << 1 * 8) + |
| 31 | + (blue << 0 * 8); |
| 32 | + |
| 33 | + const ColorData.argb(int alpha, int red, int green, int blue) |
| 34 | + : assert(red >= 0 && red <= 255), |
| 35 | + assert(green >= 0 && green <= 255), |
| 36 | + assert(blue >= 0 && blue <= 255), |
| 37 | + assert(alpha >= 0 && alpha <= 255), |
| 38 | + _hex = (alpha << 3 * 8) + |
| 39 | + (red << 2 * 8) + |
| 40 | + (green << 1 * 8) + |
| 41 | + (blue << 0 * 8); |
| 42 | + |
| 43 | + @override |
| 44 | + bool operator ==(Object other) { |
| 45 | + if (other is! ColorData) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + return _hex == other._hex; |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + int get hashCode => _hex.hashCode; |
| 53 | +} |
0 commit comments