Skip to content

Commit

Permalink
Merge branch 'flame-engine#69-dart-sdk'
Browse files Browse the repository at this point in the history
  • Loading branch information
benni-tec committed Oct 29, 2023
2 parents 93d2d81 + 8f38c1c commit aeef188
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 19 deletions.
26 changes: 23 additions & 3 deletions packages/tiled/lib/src/common/color.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
part of tiled;

const _mask = 0xff;
int _sub(int hex, int index) => (hex & (_mask << index * 8)) >> index * 8;

class Color extends RgbColor {
int _sub(int hex, int index) {
final value = (hex & (_mask << index * 8)) >> index * 8;
assert(value >= 0 && value < 256);
return value;
}

class Color {
final int red;
final int green;
final int blue;
final int alpha;

/// Format: aarrggbb
Color(int hex) : super(_sub(hex, 2), _sub(hex, 1), _sub(hex, 0), _sub(hex, 3));
Color.hex(int hex)
: alpha = _sub(hex, 3),
red = _sub(hex, 2),
green = _sub(hex, 1),
blue = _sub(hex, 0);

const Color.rgb(this.red, this.green, this.blue, [this.alpha = 255])
: assert(red >= 0 && red <= 255),
assert(green >= 0 && green <= 255),
assert(blue >= 0 && blue <= 255),
assert(alpha >= 0 && alpha <= 255);
}
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/common/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Property<T> {
case PropertyType.color:
return ColorProperty(
name: name,
value: parser.getColor('value', defaults: Color(0x00000000)),
value: parser.getColor('value', defaults: Color.hex(0x00000000)),
hexValue: parser.getString('value', defaults: '#00000000'),
);

Expand Down Expand Up @@ -156,7 +156,7 @@ class ObjectProperty extends Property<int> {
}

/// [value] is the color
class ColorProperty extends Property<ColorModel> {
class ColorProperty extends Property<Color> {
final String hexValue;

ColorProperty({
Expand Down
8 changes: 4 additions & 4 deletions packages/tiled/lib/src/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ abstract class Layer {
/// child layers (optional).
///
/// Parsed from [tintColorHex], will be null if parsing fails for any reason.
ColorModel? tintColor;
Color? tintColor;

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

class ObjectGroup extends Layer {
static const defaultColor = RgbColor(160, 160, 164, 255);
static const defaultColor = Color.rgb(160, 160, 164, 255);
static const defaultColorHex = '%a0a0a4';

/// topdown (default) or index (indexOrder).
Expand All @@ -438,7 +438,7 @@ class ObjectGroup extends Layer {
///
/// Parsed from [colorHex], will be fallback to [defaultColor] if parsing
/// fails for any reason.
ColorModel color;
Color color;

ObjectGroup({
super.id,
Expand Down Expand Up @@ -478,7 +478,7 @@ class ImageLayer extends Layer {
///
/// Parsed from [transparentColorHex], will be null if parsing fails for any
/// reason.
ColorModel? transparentColor;
Color? transparentColor;

/// Whether or not to repeat the image on the X-axis
bool repeatX;
Expand Down
6 changes: 3 additions & 3 deletions packages/tiled/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ abstract class Parser {
return result;
}

ColorModel? getColorOrNull(String name, {ColorModel? defaults}) {
Color? getColorOrNull(String name, {Color? defaults}) {
final tiledColor = getStringOrNull(name);

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

if (colorValue != null) {
return Color(colorValue);
return Color.hex(colorValue);
} else {
return defaults;
}
}

ColorModel getColor(String name, {ColorModel? defaults}) {
Color getColor(String name, {Color? defaults}) {
final result = getColorOrNull(name, defaults: defaults);
if (result == null) {
throw ParsingException(name, null, 'Missing required color field');
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tiled_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TiledMap {
///
/// Parsed from [backgroundColorHex], will be null if parsing fails for any
/// reason.
ColorModel? backgroundColor;
Color? backgroundColor;

int compressionLevel;

Expand Down
1 change: 0 additions & 1 deletion packages/tiled/lib/tiled.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:collection/collection.dart';
import 'package:color_models/color_models.dart';
import 'package:meta/meta.dart';
import 'package:xml/xml.dart';

Expand Down
1 change: 0 additions & 1 deletion packages/tiled/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ dependencies:
collection: ^1.16.0
meta: ^1.7.0
xml: ^6.1.0
color_models: ^1.3.3

dev_dependencies:
dartdoc: ^6.0.1
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/layer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void main() {
expect(layer.tintColorHex, equals('#ffaabb'));
expect(
layer.tintColor,
equals(Color(int.parse('0xffffaabb'))),
equals(Color.hex(int.parse('0xffffaabb'))),
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/object_group_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main() {
expect(objectGroup.colorHex, equals('#555500'));
expect(
objectGroup.color,
equals(Color(int.parse('0xff555500'))),
equals(Color.hex(int.parse('0xff555500'))),
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/tiled/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() {
expect(map.backgroundColorHex, equals('#ccddaaff'));
expect(
map.backgroundColor,
equals(Color(int.parse('0xccddaaff'))),
equals(Color.hex(int.parse('0xccddaaff'))),
);
});

Expand Down Expand Up @@ -100,7 +100,7 @@ void main() {
);
expect(
properties.getValue<Color>('color property'),
equals(Color(0x00112233)),
equals(Color.hex(0x00112233)),
);
expect(
properties.getValue<double>('float property'),
Expand Down

0 comments on commit aeef188

Please sign in to comment.