Skip to content

Commit

Permalink
Migrate to srgb color
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 27, 2024
1 parent f8c9168 commit 8208505
Show file tree
Hide file tree
Showing 67 changed files with 557 additions and 545 deletions.
16 changes: 16 additions & 0 deletions api/lib/src/converter/color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:dart_leap/dart_leap.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

class ColorJsonConverter extends JsonConverter<SRGBColor, int> {
const ColorJsonConverter();

@override
SRGBColor fromJson(int json) {
return SRGBColor(json);
}

@override
int toJson(SRGBColor object) {
return object.value;
}
}
17 changes: 8 additions & 9 deletions api/lib/src/converter/xopp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:typed_data';
import 'package:archive/archive.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:butterfly_api/butterfly_text.dart' as text;
import 'package:dart_leap/dart_leap.dart';
import 'package:xml/xml.dart';

List<PathPoint> toPoints(List<double> data) {
Expand All @@ -20,14 +21,12 @@ List<PathPoint> toPoints(List<double> data) {
return points;
}

int _importColor(String value) {
final number = int.parse(value.substring(1), radix: 16);
return (number >> 8 | number << 24);
SRGBColor _importColor(String value) {
return SRGBColor.parse(value);
}

String _exportColor(int value) {
final number = (value >> 8 | value << 24);
return '#${number.toRadixString(16)}';
String _exportColor(SRGBColor value) {
return value.toHexString();
}

(NoteData, PadElement?) getElement(
Expand Down Expand Up @@ -161,8 +160,8 @@ Uint8List xoppExporter(NoteData document) {
builder.element('page', nest: () {
builder.element('background', attributes: {
'type': 'solid',
'color':
_exportColor(page.backgrounds.firstOrNull?.defaultColor ?? 0),
'color': _exportColor(
page.backgrounds.firstOrNull?.defaultColor ?? SRGBColor.white),
'style': 'plain',
});
builder.element('layer', nest: () {
Expand All @@ -185,7 +184,7 @@ Uint8List xoppExporter(NoteData document) {
?.span
: styleSheet?.getParagraphProperty('p')?.span;
builder.element('text', attributes: {
'color': _exportColor(style?.color ?? 0),
'color': _exportColor(style?.color ?? SRGBColor.black),
'size': (style?.size ?? 12).toString(),
'x': e.position.x.toString(),
'y': e.position.y.toString(),
Expand Down
6 changes: 3 additions & 3 deletions api/lib/src/models/background.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:dart_leap/dart_leap.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import 'colors.dart';
import 'texture.dart';

part 'background.g.dart';
Expand Down Expand Up @@ -36,8 +36,8 @@ sealed class Background with _$Background {
factory Background.fromJson(Map<String, dynamic> json) =>
_$BackgroundFromJson(json);

int get defaultColor => switch (this) {
SRGBColor get defaultColor => switch (this) {
TextureBackground e => e.texture.boxColor,
_ => BasicColors.white
_ => SRGBColor.white
};
}
13 changes: 6 additions & 7 deletions api/lib/src/models/colors.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:dart_leap/dart_leap.dart';

class BasicColors {
static const white = 0xFFFFFFFF;
static const light = 0xFFD9D9D9;
static const black = 0xFF000000;
static const dark = 0xFF1A1A1A;
static const red = 0xFFF44336;
static const blue = 0xFF2196F3;
static const transparent = 0x00000000;
static const light = SRGBColor(0xFFD9D9D9);
static const dark = SRGBColor(0xFF1A1A1A);
static const red = SRGBColor(0xFFF44336);
static const blue = SRGBColor(0xFF2196F3);

const BasicColors._();
}
10 changes: 6 additions & 4 deletions api/lib/src/models/element.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:math';

import 'package:butterfly_api/src/converter/color.dart';
import 'package:dart_leap/dart_leap.dart';

import '../converter/core.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../converter/id.dart';
import 'colors.dart';
import 'pack.dart';
import 'point.dart';
import 'property.dart';
Expand Down Expand Up @@ -60,7 +62,7 @@ mixin LabelElement {
double get scale;
PackAssetLocation get styleSheet;
ElementConstraint get constraint;
int get foreground;
SRGBColor get foreground;

AreaProperty get areaProperty => switch (this) {
MarkdownElement e => e.areaProperty,
Expand Down Expand Up @@ -102,7 +104,7 @@ sealed class PadElement with _$PadElement {
@Default(PackAssetLocation()) PackAssetLocation styleSheet,
required TextArea area,
@Default(ElementConstraint(size: 1000)) ElementConstraint constraint,
@Default(BasicColors.black) int foreground,
@Default(SRGBColor.black) @ColorJsonConverter() SRGBColor foreground,
@Default({}) Map<String, dynamic> extra,
}) = TextElement;

Expand All @@ -119,7 +121,7 @@ sealed class PadElement with _$PadElement {
@Default(AreaProperty()) AreaProperty areaProperty,
required String text,
@Default(ElementConstraint(size: 1000)) ElementConstraint constraint,
@Default(BasicColors.black) int foreground,
@Default(SRGBColor.black) @ColorJsonConverter() SRGBColor foreground,
@Default({}) Map<String, dynamic> extra,
}) = MarkdownElement;

Expand Down
28 changes: 16 additions & 12 deletions api/lib/src/models/element.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ abstract class _$$TextElementImplCopyWith<$Res>
PackAssetLocation styleSheet,
TextArea area,
ElementConstraint constraint,
int foreground,
@ColorJsonConverter() SRGBColor foreground,
Map<String, dynamic> extra});

$PackAssetLocationCopyWith<$Res> get styleSheet;
Expand Down Expand Up @@ -978,7 +978,7 @@ class __$$TextElementImplCopyWithImpl<$Res>
foreground: null == foreground
? _value.foreground
: foreground // ignore: cast_nullable_to_non_nullable
as int,
as SRGBColor,
extra: null == extra
? _value._extra
: extra // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -1029,7 +1029,7 @@ class _$TextElementImpl extends TextElement with LabelElement {
this.styleSheet = const PackAssetLocation(),
required this.area,
this.constraint = const ElementConstraint(size: 1000),
this.foreground = BasicColors.black,
@ColorJsonConverter() this.foreground = SRGBColor.black,
final Map<String, dynamic> extra = const {},
final String? $type})
: _extra = extra,
Expand Down Expand Up @@ -1065,7 +1065,8 @@ class _$TextElementImpl extends TextElement with LabelElement {
final ElementConstraint constraint;
@override
@JsonKey()
final int foreground;
@ColorJsonConverter()
final SRGBColor foreground;
final Map<String, dynamic> _extra;
@override
@JsonKey()
Expand Down Expand Up @@ -1109,7 +1110,7 @@ abstract class TextElement extends PadElement implements LabelElement {
final PackAssetLocation styleSheet,
required final TextArea area,
final ElementConstraint constraint,
final int foreground,
@ColorJsonConverter() final SRGBColor foreground,
final Map<String, dynamic> extra}) = _$TextElementImpl;
TextElement._() : super._();

Expand All @@ -1129,7 +1130,8 @@ abstract class TextElement extends PadElement implements LabelElement {
PackAssetLocation get styleSheet;
TextArea get area;
ElementConstraint get constraint;
int get foreground;
@ColorJsonConverter()
SRGBColor get foreground;
@override
Map<String, dynamic> get extra;

Expand Down Expand Up @@ -1159,7 +1161,7 @@ abstract class _$$MarkdownElementImplCopyWith<$Res>
AreaProperty areaProperty,
String text,
ElementConstraint constraint,
int foreground,
@ColorJsonConverter() SRGBColor foreground,
Map<String, dynamic> extra});

$PackAssetLocationCopyWith<$Res> get styleSheet;
Expand Down Expand Up @@ -1232,7 +1234,7 @@ class __$$MarkdownElementImplCopyWithImpl<$Res>
foreground: null == foreground
? _value.foreground
: foreground // ignore: cast_nullable_to_non_nullable
as int,
as SRGBColor,
extra: null == extra
? _value._extra
: extra // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -1284,7 +1286,7 @@ class _$MarkdownElementImpl extends MarkdownElement with LabelElement {
this.areaProperty = const AreaProperty(),
required this.text,
this.constraint = const ElementConstraint(size: 1000),
this.foreground = BasicColors.black,
@ColorJsonConverter() this.foreground = SRGBColor.black,
final Map<String, dynamic> extra = const {},
final String? $type})
: _extra = extra,
Expand Down Expand Up @@ -1323,7 +1325,8 @@ class _$MarkdownElementImpl extends MarkdownElement with LabelElement {
final ElementConstraint constraint;
@override
@JsonKey()
final int foreground;
@ColorJsonConverter()
final SRGBColor foreground;
final Map<String, dynamic> _extra;
@override
@JsonKey()
Expand Down Expand Up @@ -1369,7 +1372,7 @@ abstract class MarkdownElement extends PadElement implements LabelElement {
final AreaProperty areaProperty,
required final String text,
final ElementConstraint constraint,
final int foreground,
@ColorJsonConverter() final SRGBColor foreground,
final Map<String, dynamic> extra}) = _$MarkdownElementImpl;
MarkdownElement._() : super._();

Expand All @@ -1390,7 +1393,8 @@ abstract class MarkdownElement extends PadElement implements LabelElement {
AreaProperty get areaProperty;
String get text;
ElementConstraint get constraint;
int get foreground;
@ColorJsonConverter()
SRGBColor get foreground;
@override
Map<String, dynamic> get extra;

Expand Down
14 changes: 10 additions & 4 deletions api/lib/src/models/element.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion api/lib/src/models/palette.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:butterfly_api/src/converter/color.dart';
import 'package:dart_leap/dart_leap.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'palette.g.dart';
Expand All @@ -6,7 +8,9 @@ part 'palette.freezed.dart';
@freezed
sealed class ColorPalette with _$ColorPalette {
const factory ColorPalette(
{required String name, @Default([]) List<int> colors}) = _ColorPalette;
{required String name,
@Default([]) @ColorJsonConverter() List<SRGBColor> colors}) =
_ColorPalette;
factory ColorPalette.fromJson(Map<String, dynamic> json) =>
_$ColorPaletteFromJson(json);
}
Loading

0 comments on commit 8208505

Please sign in to comment.