Skip to content

Commit f697800

Browse files
author
benni-tec
committed
renamed Color to ColorData to make conversion easier in flame_tiled // added tests for hex decoding in ColorData
1 parent 8f38c1c commit f697800

File tree

9 files changed

+48
-29
lines changed

9 files changed

+48
-29
lines changed

packages/tiled/lib/src/common/color.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
part of tiled;
22

3-
const _mask = 0xff;
3+
class ColorData {
4+
static int _sub(int hex, int index) => (hex >> index * 8) & 0x000000ff;
45

5-
int _sub(int hex, int index) {
6-
final value = (hex & (_mask << index * 8)) >> index * 8;
7-
assert(value >= 0 && value < 256);
8-
return value;
9-
}
10-
11-
class Color {
126
final int red;
137
final int green;
148
final int blue;
159
final int alpha;
1610

1711
/// Format: aarrggbb
18-
Color.hex(int hex)
12+
ColorData.hex(int hex)
1913
: alpha = _sub(hex, 3),
2014
red = _sub(hex, 2),
2115
green = _sub(hex, 1),
2216
blue = _sub(hex, 0);
2317

24-
const Color.rgb(this.red, this.green, this.blue, [this.alpha = 255])
18+
const ColorData.rgb(this.red, this.green, this.blue, [this.alpha = 255])
2519
: assert(red >= 0 && red <= 255),
2620
assert(green >= 0 && green <= 255),
2721
assert(blue >= 0 && blue <= 255),

packages/tiled/lib/src/common/property.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Property<T> {
3737
case PropertyType.color:
3838
return ColorProperty(
3939
name: name,
40-
value: parser.getColor('value', defaults: Color.hex(0x00000000)),
40+
value: parser.getColor('value', defaults: ColorData.hex(0x00000000)),
4141
hexValue: parser.getString('value', defaults: '#00000000'),
4242
);
4343

@@ -156,7 +156,7 @@ class ObjectProperty extends Property<int> {
156156
}
157157

158158
/// [value] is the color
159-
class ColorProperty extends Property<Color> {
159+
class ColorProperty extends Property<ColorData> {
160160
final String hexValue;
161161

162162
ColorProperty({

packages/tiled/lib/src/layer.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ abstract class Layer {
7777
/// any graphics drawn by this layer or any child layers (optional).
7878
String? tintColorHex;
7979

80-
/// [Color] that is multiplied with any graphics drawn by this layer or any
80+
/// [ColorData] that is multiplied with any graphics drawn by this layer or any
8181
/// child layers (optional).
8282
///
8383
/// Parsed from [tintColorHex], will be null if parsing fails for any reason.
84-
Color? tintColor;
84+
ColorData? tintColor;
8585

8686
/// The opacity of the layer as a value from 0 to 1. Defaults to 1.
8787
double opacity;
@@ -420,7 +420,7 @@ class TileLayer extends Layer {
420420
}
421421

422422
class ObjectGroup extends Layer {
423-
static const defaultColor = Color.rgb(160, 160, 164, 255);
423+
static const defaultColor = ColorData.rgb(160, 160, 164, 255);
424424
static const defaultColorHex = '%a0a0a4';
425425

426426
/// topdown (default) or index (indexOrder).
@@ -433,12 +433,12 @@ class ObjectGroup extends Layer {
433433
/// this group. (defaults to gray (“#a0a0a4”))
434434
String colorHex;
435435

436-
/// [Color] used to display the objects in this group.
436+
/// [ColorData] used to display the objects in this group.
437437
/// (defaults to gray (“#a0a0a4”))
438438
///
439439
/// Parsed from [colorHex], will be fallback to [defaultColor] if parsing
440440
/// fails for any reason.
441-
Color color;
441+
ColorData color;
442442

443443
ObjectGroup({
444444
super.id,
@@ -474,11 +474,11 @@ class ImageLayer extends Layer {
474474
/// (optional).
475475
String? transparentColorHex;
476476

477-
/// [Color] to be rendered as transparent (optional).
477+
/// [ColorData] to be rendered as transparent (optional).
478478
///
479479
/// Parsed from [transparentColorHex], will be null if parsing fails for any
480480
/// reason.
481-
Color? transparentColor;
481+
ColorData? transparentColor;
482482

483483
/// Whether or not to repeat the image on the X-axis
484484
bool repeatX;

packages/tiled/lib/src/parser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ abstract class Parser {
197197
return result;
198198
}
199199

200-
Color? getColorOrNull(String name, {Color? defaults}) {
200+
ColorData? getColorOrNull(String name, {ColorData? defaults}) {
201201
final tiledColor = getStringOrNull(name);
202202

203203
// Tiled colors are stored as either ARGB or RGB hex values, so we can
@@ -212,13 +212,13 @@ abstract class Parser {
212212
}
213213

214214
if (colorValue != null) {
215-
return Color.hex(colorValue);
215+
return ColorData.hex(colorValue);
216216
} else {
217217
return defaults;
218218
}
219219
}
220220

221-
Color getColor(String name, {Color? defaults}) {
221+
ColorData getColor(String name, {ColorData? defaults}) {
222222
final result = getColorOrNull(name, defaults: defaults);
223223
if (result == null) {
224224
throw ParsingException(name, null, 'Missing required color field');

packages/tiled/lib/src/tiled_map.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ class TiledMap {
7474
/// behind all other layers (optional).
7575
String? backgroundColorHex;
7676

77-
/// [Color] to be rendered as a solid color behind all other layers
77+
/// [ColorData] to be rendered as a solid color behind all other layers
7878
/// (optional).
7979
///
8080
/// Parsed from [backgroundColorHex], will be null if parsing fails for any
8181
/// reason.
82-
Color? backgroundColor;
82+
ColorData? backgroundColor;
8383

8484
int compressionLevel;
8585

packages/tiled/test/color.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:math';
2+
3+
import 'package:test/expect.dart';
4+
import 'package:test/scaffolding.dart';
5+
import 'package:tiled/tiled.dart';
6+
7+
void main() {
8+
group("ColorData.hex", () {
9+
test("parse", () {
10+
final random = Random();
11+
final red = random.nextInt(256);
12+
final green = random.nextInt(256);
13+
final blue = random.nextInt(256);
14+
final alpha = random.nextInt(256);
15+
16+
final hex = alpha << 24 | red << 16 | green << 8 | blue << 0;
17+
final data = ColorData.hex(hex);
18+
19+
expect(data.alpha, equals(alpha));
20+
expect(data.red, equals(red));
21+
expect(data.green, equals(green));
22+
expect(data.blue, equals(blue));
23+
});
24+
});
25+
}

packages/tiled/test/layer_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void main() {
7474
expect(layer.tintColorHex, equals('#ffaabb'));
7575
expect(
7676
layer.tintColor,
77-
equals(Color.hex(int.parse('0xffffaabb'))),
77+
equals(ColorData.hex(int.parse('0xffffaabb'))),
7878
);
7979
});
8080
});

packages/tiled/test/object_group_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void main() {
3030
expect(objectGroup.colorHex, equals('#555500'));
3131
expect(
3232
objectGroup.color,
33-
equals(Color.hex(int.parse('0xff555500'))),
33+
equals(ColorData.hex(int.parse('0xff555500'))),
3434
);
3535
});
3636

packages/tiled/test/parser_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void main() {
4343
expect(map.backgroundColorHex, equals('#ccddaaff'));
4444
expect(
4545
map.backgroundColor,
46-
equals(Color.hex(int.parse('0xccddaaff'))),
46+
equals(ColorData.hex(int.parse('0xccddaaff'))),
4747
);
4848
});
4949

@@ -99,8 +99,8 @@ void main() {
9999
equals('#00112233'),
100100
);
101101
expect(
102-
properties.getValue<Color>('color property'),
103-
equals(Color.hex(0x00112233)),
102+
properties.getValue<ColorData>('color property'),
103+
equals(ColorData.hex(0x00112233)),
104104
);
105105
expect(
106106
properties.getValue<double>('float property'),

0 commit comments

Comments
 (0)