Skip to content

Commit

Permalink
Merge pull request #64 from lichess-org/colors_adjustment
Browse files Browse the repository at this point in the history
Colors adjustment
  • Loading branch information
veloce authored Dec 19, 2024
2 parents 5445feb + b87da63 commit 617b886
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 36 deletions.
42 changes: 21 additions & 21 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63
url: "https://pub.dev"
source: hosted
version: "2.11.0"
version: "2.12.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
characters:
dependency: transitive
description:
Expand All @@ -31,15 +31,15 @@ packages:
path: ".."
relative: true
source: path
version: "6.1.0"
version: "6.2.0"
clock:
dependency: transitive
description:
name: clock
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b
url: "https://pub.dev"
source: hosted
version: "1.1.1"
version: "1.1.2"
collection:
dependency: transitive
description:
Expand All @@ -60,18 +60,18 @@ packages:
dependency: "direct main"
description:
name: dartchess
sha256: "9ce59d164b5ddf87fdb3cbe118015674f2f241e33b0891cec1e9f30b68a81b26"
sha256: "6ddc173288cb63444edf0d766986c283bb1ae6d8ede2ac1fc9de857f0f84d051"
url: "https://pub.dev"
source: hosted
version: "0.9.0"
version: "0.9.2"
fake_async:
dependency: transitive
description:
name: fake_async
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
sha256: "6a95e56b2449df2273fd8c45a662d6947ce1ebb7aafe80e550a3f68297f3cacc"
url: "https://pub.dev"
source: hosted
version: "1.3.1"
version: "1.3.2"
fast_immutable_collections:
dependency: transitive
description:
Expand Down Expand Up @@ -102,18 +102,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec
url: "https://pub.dev"
source: hosted
version: "10.0.7"
version: "10.0.8"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
url: "https://pub.dev"
source: hosted
version: "3.0.8"
version: "3.0.9"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -158,10 +158,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
version: "1.9.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -195,10 +195,10 @@ packages:
dependency: transitive
description:
name: string_scanner
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
sha256: "0bd04f5bb74fcd6ff0606a888a30e917af9bd52820b178eaa464beb11dca84b6"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.4.0"
term_glyph:
dependency: transitive
description:
Expand Down Expand Up @@ -227,10 +227,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14"
url: "https://pub.dev"
source: hosted
version: "14.3.0"
version: "14.3.1"
sdks:
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
12 changes: 12 additions & 0 deletions lib/src/board_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class ChessboardSettings {
this.border,
this.borderRadius = BorderRadius.zero,
this.boxShadow = const <BoxShadow>[],
this.brightness = 0.0,
this.hue = 0.0,
this.enableCoordinates = true,
this.animationDuration = const Duration(milliseconds: 250),
this.showLastMove = true,
Expand Down Expand Up @@ -130,6 +132,12 @@ class ChessboardSettings {
/// Box shadow of the board
final List<BoxShadow> boxShadow;

/// Brightness adjustment of the board
final double brightness;

/// Hue adjustment of the board
final double hue;

/// Whether to show board coordinates
final bool enableCoordinates;

Expand Down Expand Up @@ -229,6 +237,8 @@ class ChessboardSettings {

ChessboardSettings copyWith({
ChessboardColorScheme? colorScheme,
double? brightness,
double? hue,
PieceAssets? pieceAssets,
BorderRadiusGeometry? borderRadius,
List<BoxShadow>? boxShadow,
Expand All @@ -249,6 +259,8 @@ class ChessboardSettings {
}) {
return ChessboardSettings(
colorScheme: colorScheme ?? this.colorScheme,
brightness: brightness ?? this.brightness,
hue: hue ?? this.hue,
pieceAssets: pieceAssets ?? this.pieceAssets,
border: border,
borderRadius: borderRadius ?? this.borderRadius,
Expand Down
29 changes: 18 additions & 11 deletions lib/src/widgets/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/widgets.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';

import 'board_border.dart';
import 'change_colors.dart';
import 'piece.dart';
import 'highlight.dart';
import 'positioned_square.dart';
Expand Down Expand Up @@ -401,17 +402,23 @@ class _BoardState extends State<Chessboard> {
),
);

if (settings.border != null) {
return BorderedChessboard(
size: widget.size,
orientation: widget.orientation,
border: settings.border!,
showCoordinates: settings.enableCoordinates,
child: board,
);
}

return board;
final borderedChessboard = settings.border != null
? BorderedChessboard(
size: widget.size,
orientation: widget.orientation,
border: settings.border!,
showCoordinates: settings.enableCoordinates,
child: board,
)
: board;

return widget.settings.hue != 0 || widget.settings.brightness != 0
? ChangeColors(
hue: widget.settings.hue,
brightness: widget.settings.brightness,
child: borderedChessboard,
)
: borderedChessboard;
}

@override
Expand Down
18 changes: 16 additions & 2 deletions lib/src/widgets/board_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../board_settings.dart';
import '../models.dart';
import '../fen.dart';
import 'board_border.dart';
import 'change_colors.dart';
import 'highlight.dart';
import 'piece.dart';
import 'positioned_square.dart';
Expand Down Expand Up @@ -231,17 +232,30 @@ class _BoardEditorState extends State<ChessboardEditor> {
),
);

final coloredBoard =
widget.settings.hue != 0 || widget.settings.brightness != 0
? ChangeColors(
hue: widget.settings.hue,
brightness: widget.settings.brightness,
child: board,
)
: board;

if (widget.settings.border != null) {
return BorderedChessboard(
size: widget.size,
orientation: widget.orientation,
border: widget.settings.border!,
showCoordinates: widget.settings.enableCoordinates,
child: board,
child: coloredBoard,
);
}

return board;
return ChangeColors(
brightness: widget.settings.brightness,
hue: widget.settings.hue,
child: coloredBoard,
);
}

void _onTouchedEvent(Offset localPosition) {
Expand Down
100 changes: 100 additions & 0 deletions lib/src/widgets/change_colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'dart:math';

import 'package:flutter/material.dart';

// Based upon: https://stackoverflow.com/questions/64639589/how-to-adjust-hue-saturation-and-brightness-of-an-image-in-flutter
// from BananaNeil: https://stackoverflow.com/users/937841/banananeil.
// This is, in turn, based upon: https://stackoverflow.com/a/7917978/937841

/// Use the [ChangeColors] widget to change the brightness and hue of any widget, including images.
///
/// Example:
///
/// ```
/// ChangeColors(
/// hue: 0.55,
/// brightness: 0.2,
/// child: Image.asset('myImage.png'),
/// );
/// ```
class ChangeColors extends StatelessWidget {
const ChangeColors({
this.brightness = 0.0,
this.hue = 0.0,
required this.child,
super.key,
});

/// Negative value will make it darker (-1 is darkest).
/// Positive value will make it lighter (1 is the maximum, but you can go above it).
/// Note: 0.0 is unchanged.
final double brightness;

/// From -1.0 to 1.0 (Note: 1.0 wraps into -1.0, such as 1.2 is the same as -0.8).
/// Note: 0.0 is unchanged. Adding or subtracting multiples of 2.0 also keeps it unchanged.
final double hue;

final Widget child;

@override
Widget build(BuildContext context) {
return ColorFiltered(
colorFilter: ColorFilter.matrix(
_adjustMatrix(hue: hue * pi, brightness: brightness),
),
child: child,
);
}
}

List<double> _adjustMatrix({required double hue, required double brightness}) {
if (hue == 0 && brightness == 0) {
// dart format off
return [
1,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
1,
0,
];
// dart format on
}
final brightnessValue = brightness <= 0 ? brightness * 100 : brightness * 100;
return List<double>.from(<double>[
0.213 + cos(hue) * 0.787 + sin(hue) * -0.213,
0.715 + cos(hue) * -0.715 + sin(hue) * -0.715,
0.072 + cos(hue) * -0.072 + sin(hue) * 0.928,
0,
brightnessValue,
0.213 + cos(hue) * -0.213 + sin(hue) * 0.143,
0.715 + cos(hue) * 0.285 + sin(hue) * 0.140,
0.072 + cos(hue) * -0.072 + sin(hue) * -0.283,
0,
brightnessValue,
0.213 + cos(hue) * -0.213 + sin(hue) * -0.787,
0.715 + cos(hue) * -0.715 + sin(hue) * 0.715,
0.072 + cos(hue) * 0.928 + sin(hue) * 0.072,
0,
brightnessValue,
0,
0,
0,
1,
0,
]).map((i) => i).toList();
}
Loading

0 comments on commit 617b886

Please sign in to comment.