Skip to content

Commit 53fa3a2

Browse files
committed
Fix canvas fill condition not working when there's only 1 fill
1 parent e896d4b commit 53fa3a2

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed

lib/src/api/models/color_rgb.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class ColorRGB extends Equatable with SerializableMixin {
6868
/// Factory constructor for creating a new [ColorRGB] instance from JSON data.
6969
factory ColorRGB.fromJson(Map json) => _$ColorRGBFromJson(json);
7070

71+
static ColorRGB? fromColorRGBA(ColorRGBA? color) {
72+
if (color == null) return null;
73+
return ColorRGB(r: color.r, g: color.g, b: color.b);
74+
}
75+
7176
@override
7277
Map toJson() => _$ColorRGBToJson(this);
7378

lib/src/api/models/color_rgba.dart

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:equatable/equatable.dart';
66
import 'package:json_annotation/json_annotation.dart';
77

8-
import '../mixins.dart';
8+
import '../../../codelessly_api.dart';
99

1010
part 'color_rgba.g.dart';
1111

@@ -81,6 +81,43 @@ class ColorRGBA with EquatableMixin, SerializableMixin {
8181
/// Factory constructor for creating a new [ColorRGBA] instance form JSON data.
8282
factory ColorRGBA.fromJson(Map json) => _$ColorRGBAFromJson(json);
8383

84+
/// Factory constructor for creating a new [ColorRGBA] instance form JSON data.
85+
/// Returns null if cannot parse the JSON.
86+
static ColorRGBA? tryFromJson(Map? json) {
87+
if (json == null) return null;
88+
try {
89+
return ColorRGBA.fromJson(json);
90+
} catch (e) {
91+
return null;
92+
}
93+
}
94+
95+
/// Factory constructor for creating a new [ColorRGBA] instance from hex string.
96+
static ColorRGBA? fromHex(String hex) {
97+
String hexCode = hex.replaceAll('#', '').toUpperCase();
98+
if (hexCode.length == 6) {
99+
hexCode = 'FF$hexCode';
100+
}
101+
102+
if (!RegExp(r'^[0-9A-F]{8}$', caseSensitive: false).hasMatch(hexCode)) {
103+
return null;
104+
}
105+
106+
return ColorRGBA(
107+
r: int.parse(hexCode.substring(2, 4), radix: 16) / 255,
108+
g: int.parse(hexCode.substring(4, 6), radix: 16) / 255,
109+
b: int.parse(hexCode.substring(6, 8), radix: 16) / 255,
110+
a: int.parse(hexCode.substring(0, 2), radix: 16) / 255,
111+
);
112+
}
113+
84114
@override
85115
Map toJson() => _$ColorRGBAToJson(this);
116+
117+
/// Creates a [PaintModel] with this color.
118+
PaintModel toPaint([String? id]) => PaintModel.solid(
119+
id: id ?? generateId(),
120+
color: ColorRGB.fromColorRGBA(this),
121+
opacity: a,
122+
);
86123
}

lib/src/api/models/variables_model.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,25 @@ enum VariableType {
1919
/// Boolean type. Represents a boolean value.
2020
boolean,
2121

22+
/// Color type. Represents a color.
23+
color,
24+
2225
/// List type. Represents a list of values.
2326
list,
2427

2528
/// Map type. Represents a map.
2629
map;
2730

2831
/// Returns a string representation of the variable type.
29-
String get label {
30-
switch (this) {
31-
case VariableType.integer:
32-
return 'Integer';
33-
case VariableType.text:
34-
return 'Text';
35-
case VariableType.decimal:
36-
return 'Decimal';
37-
case VariableType.boolean:
38-
return 'Boolean';
39-
case VariableType.list:
40-
return 'List';
41-
case VariableType.map:
42-
return 'Map';
43-
}
44-
}
32+
String get label => switch (this) {
33+
VariableType.integer => 'Integer',
34+
VariableType.text => 'Text',
35+
VariableType.decimal => 'Decimal',
36+
VariableType.boolean => 'Boolean',
37+
VariableType.color => 'Color',
38+
VariableType.list => 'List',
39+
VariableType.map => 'Map',
40+
};
4541
}
4642

4743
/// Store information of a variable. [id] must not be empty when creating a
@@ -125,6 +121,7 @@ class VariableData
125121
VariableType.integer => num.tryParse(value).toInt(),
126122
VariableType.decimal => num.tryParse(value).toDouble(),
127123
VariableType.boolean => bool.tryParse(value, caseSensitive: false),
124+
VariableType.color => ColorRGBA.fromHex(value),
128125
VariableType.map => tryJsonDecode(value),
129126
VariableType.list => value.toList(),
130127
};
@@ -140,6 +137,10 @@ String? sanitizeValueForVariableType(String? value, VariableType type) {
140137
VariableType.decimal => num.tryParse(value).toDouble()?.toString(),
141138
VariableType.boolean =>
142139
bool.tryParse(value, caseSensitive: false)?.toString(),
140+
VariableType.color =>
141+
RegExp(r'^#[0-9a-fA-F]{2,8}$', caseSensitive: false).hasMatch(value)
142+
? value.toUpperCase()
143+
: null,
143144
// TODO: this could be a bit expensive. Maybe enable only when required!
144145
// VariableType.map => tryJsonDecode(value),
145146
// VariableType.list => value.toList(),

lib/src/api/models/variables_model.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/typed_value.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'dart:convert';
22

33
import 'package:collection/collection.dart';
44

5+
import '../../codelessly_api.dart';
6+
57
/// Declares extensions on [Object].
68
extension ObjectExt on Object? {
79
/// Converts given string to a typed value if possible.
@@ -17,6 +19,10 @@ extension ObjectExt on Object? {
1719
double => value.toDouble().tryCast<R>() ?? defaultValue,
1820
num => value.toNum().tryCast<R>() ?? defaultValue,
1921
bool => value.toBool().tryCast<R>() ?? defaultValue,
22+
ColorRGBA => value.toColorRGBA().tryCast<R>() ?? defaultValue,
23+
ColorRGB => ColorRGB.fromColorRGBA(value.toColorRGBA()).tryCast<R>() ??
24+
defaultValue,
25+
PaintModel => value.toColorRGBA()?.toPaint().tryCast<R>() ?? defaultValue,
2026
_ when R.isMap => value.toMap().tryCast<R>(),
2127
_ when R.isList || R.isIterable => value.toList<R>(),
2228
_ when R.isSet => value.toSet<R>(),
@@ -163,6 +169,17 @@ extension ConversionExt on Object? {
163169
return null;
164170
}
165171

172+
/// Converts given object to [ColorRGBA] if possible. Returns null otherwise.
173+
ColorRGBA? toColorRGBA() {
174+
final value = this;
175+
if (value == null) return null;
176+
if (value is ColorRGBA) return value;
177+
if (value is String) return ColorRGBA.fromHex(value);
178+
if (value is ColorRGB) return value.toColorRGBA();
179+
180+
return null;
181+
}
182+
166183
/// Converts given object to a map if possible. Returns null otherwise.
167184
Map? toMap() {
168185
if (this == null) return null;

0 commit comments

Comments
 (0)