/
main.dart
306 lines (274 loc) · 9.09 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:palette_generator/palette_generator.dart';
void main() => runApp(const MyApp());
const Color _kBackgroundColor = Color(0xffa0a0a0);
const Color _kSelectionRectangleBackground = Color(0x15000000);
const Color _kSelectionRectangleBorder = Color(0x80000000);
const Color _kPlaceholderColor = Color(0x80404040);
/// The main Application class.
class MyApp extends StatelessWidget {
/// Creates the main Application class.
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image Colors',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const ImageColors(
title: 'Image Colors',
image: AssetImage('assets/landscape.png'),
imageSize: Size(256.0, 170.0),
),
);
}
}
/// The home page for this example app.
@immutable
class ImageColors extends StatefulWidget {
/// Creates the home page.
const ImageColors({
super.key,
this.title,
required this.image,
this.imageSize,
});
/// The title that is shown at the top of the page.
final String? title;
/// This is the image provider that is used to load the colors from.
final ImageProvider image;
/// The dimensions of the image.
final Size? imageSize;
@override
State<ImageColors> createState() {
return _ImageColorsState();
}
}
class _ImageColorsState extends State<ImageColors> {
Rect? region;
Rect? dragRegion;
Offset? startDrag;
Offset? currentDrag;
PaletteGenerator? paletteGenerator;
final GlobalKey imageKey = GlobalKey();
@override
void initState() {
super.initState();
if (widget.imageSize != null) {
region = Offset.zero & widget.imageSize!;
}
_updatePaletteGenerator(region);
}
Future<void> _updatePaletteGenerator(Rect? newRegion) async {
paletteGenerator = await PaletteGenerator.fromImageProvider(
widget.image,
size: widget.imageSize,
region: newRegion,
maximumColorCount: 20,
);
setState(() {});
}
// Called when the user starts to drag
void _onPanDown(DragDownDetails details) {
final RenderBox box =
imageKey.currentContext!.findRenderObject()! as RenderBox;
final Offset localPosition = box.globalToLocal(details.globalPosition);
setState(() {
startDrag = localPosition;
currentDrag = localPosition;
dragRegion = Rect.fromPoints(localPosition, localPosition);
});
}
// Called as the user drags: just updates the region, not the colors.
void _onPanUpdate(DragUpdateDetails details) {
setState(() {
currentDrag = currentDrag! + details.delta;
dragRegion = Rect.fromPoints(startDrag!, currentDrag!);
});
}
// Called if the drag is canceled (e.g. by rotating the device or switching
// apps)
void _onPanCancel() {
setState(() {
dragRegion = null;
startDrag = null;
});
}
// Called when the drag ends. Sets the region, and updates the colors.
Future<void> _onPanEnd(DragEndDetails details) async {
final Size? imageSize = imageKey.currentContext?.size;
Rect? newRegion;
if (imageSize != null) {
newRegion = (Offset.zero & imageSize).intersect(dragRegion!);
if (newRegion.size.width < 4 && newRegion.size.width < 4) {
newRegion = Offset.zero & imageSize;
}
}
await _updatePaletteGenerator(newRegion);
setState(() {
region = newRegion;
dragRegion = null;
startDrag = null;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: _kBackgroundColor,
appBar: AppBar(
title: Text(widget.title ?? ''),
),
body: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20.0),
// GestureDetector is used to handle the selection rectangle.
child: GestureDetector(
onPanDown: _onPanDown,
onPanUpdate: _onPanUpdate,
onPanCancel: _onPanCancel,
onPanEnd: _onPanEnd,
child: Stack(children: <Widget>[
Image(
key: imageKey,
image: widget.image,
width: widget.imageSize?.width,
height: widget.imageSize?.height,
),
// This is the selection rectangle.
Positioned.fromRect(
rect: dragRegion ?? region ?? Rect.zero,
child: Container(
decoration: BoxDecoration(
color: _kSelectionRectangleBackground,
border: Border.all(
color: _kSelectionRectangleBorder,
)),
)),
]),
),
),
// Use a FutureBuilder so that the palettes will be displayed when
// the palette generator is done generating its data.
PaletteSwatches(generator: paletteGenerator),
],
),
);
}
}
/// A widget that draws the swatches for the [PaletteGenerator] it is given,
/// and shows the selected target colors.
class PaletteSwatches extends StatelessWidget {
/// Create a Palette swatch.
///
/// The [generator] is optional. If it is null, then the display will
/// just be an empty container.
const PaletteSwatches({super.key, this.generator});
/// The [PaletteGenerator] that contains all of the swatches that we're going
/// to display.
final PaletteGenerator? generator;
@override
Widget build(BuildContext context) {
final List<Widget> swatches = <Widget>[];
final PaletteGenerator? paletteGen = generator;
if (paletteGen == null || paletteGen.colors.isEmpty) {
return Container();
}
for (final Color color in paletteGen.colors) {
swatches.add(PaletteSwatch(color: color));
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Wrap(
children: swatches,
),
Container(height: 30.0),
PaletteSwatch(
label: 'Dominant', color: paletteGen.dominantColor?.color),
PaletteSwatch(
label: 'Light Vibrant', color: paletteGen.lightVibrantColor?.color),
PaletteSwatch(label: 'Vibrant', color: paletteGen.vibrantColor?.color),
PaletteSwatch(
label: 'Dark Vibrant', color: paletteGen.darkVibrantColor?.color),
PaletteSwatch(
label: 'Light Muted', color: paletteGen.lightMutedColor?.color),
PaletteSwatch(label: 'Muted', color: paletteGen.mutedColor?.color),
PaletteSwatch(
label: 'Dark Muted', color: paletteGen.darkMutedColor?.color),
],
);
}
}
/// A small square of color with an optional label.
@immutable
class PaletteSwatch extends StatelessWidget {
/// Creates a PaletteSwatch.
///
/// If the [paletteColor] has property `isTargetColorFound` as `false`,
/// then the swatch will show a placeholder instead, to indicate
/// that there is no color.
const PaletteSwatch({
super.key,
this.color,
this.label,
});
/// The color of the swatch.
final Color? color;
/// The optional label to display next to the swatch.
final String? label;
@override
Widget build(BuildContext context) {
// Compute the "distance" of the color swatch and the background color
// so that we can put a border around those color swatches that are too
// close to the background's saturation and lightness. We ignore hue for
// the comparison.
final HSLColor hslColor = HSLColor.fromColor(color ?? Colors.transparent);
final HSLColor backgroundAsHsl = HSLColor.fromColor(_kBackgroundColor);
final double colorDistance = math.sqrt(
math.pow(hslColor.saturation - backgroundAsHsl.saturation, 2.0) +
math.pow(hslColor.lightness - backgroundAsHsl.lightness, 2.0));
Widget swatch = Padding(
padding: const EdgeInsets.all(2.0),
child: color == null
? const Placeholder(
fallbackWidth: 34.0,
fallbackHeight: 20.0,
color: Color(0xff404040),
)
: Container(
decoration: BoxDecoration(
color: color,
border: Border.all(
color: _kPlaceholderColor,
style: colorDistance < 0.2
? BorderStyle.solid
: BorderStyle.none,
)),
width: 34.0,
height: 20.0,
),
);
if (label != null) {
swatch = ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 130.0, minWidth: 130.0),
child: Row(
children: <Widget>[
swatch,
Container(width: 5.0),
Text(label!),
],
),
);
}
return swatch;
}
}