Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/flame/rendering/palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ members:
- `paint`: creates a new `Paint` with the color specified. `Paint` is a non-`const` class, so this
method actually creates a brand new instance every time it's called. It's safe to cascade
mutations to this.

It also exposes helper methods to derive a new `PaletteEntry` by transforming the underlying color,
such as `withRed` or `lighten`.
10 changes: 10 additions & 0 deletions packages/flame/lib/src/palette.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:ui';

import 'package:flame/extensions.dart';

class PaletteEntry {
final Color color;

Expand All @@ -22,6 +24,14 @@ class PaletteEntry {
PaletteEntry withBlue(int blue) {
return PaletteEntry(color.withBlue(blue));
}

PaletteEntry darken(double amount) {
return PaletteEntry(color.darken(amount));
}

PaletteEntry brighten(double amount) {
return PaletteEntry(color.brighten(amount));
}
}

class BasicPalette {
Expand Down
22 changes: 22 additions & 0 deletions packages/flame/test/palette_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flame/palette.dart';
import 'package:test/test.dart';

void main() {
group('Palette', () {
test('updates color', () {
const entry = BasicPalette.red;
expect(entry.color, const Color(0xFFFF0000));
expect(entry.withAlpha(100).color, const Color(0x64FF0000));
expect(entry.withRed(100).color, const Color(0xFF640000));
expect(entry.withGreen(100).color, const Color(0xFFFF6400));
expect(entry.withBlue(100).color, const Color(0xFFFF0064));
});

test('darkens and brightens color', () {
const entry = BasicPalette.red;
expect(entry.color, const Color(0xFFFF0000));
expect(entry.darken(0.5).color, const Color(0xFF800000));
expect(entry.brighten(0.5).color, const Color(0xFFFF8080));
});
});
}
Loading