Skip to content

Commit 3252cbf

Browse files
authored
Merge pull request #8 from kc3hack/generate_color_schema
Generate_color_schema
2 parents efde5e7 + b3bb42a commit 3252cbf

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ class MitakaApp extends HookConsumerWidget {
2929
routerConfig: goRouter,
3030
);
3131
}
32-
}
32+
}

lib/theme/custom_color_schema.dart

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Flutter imports:
2+
import 'package:flutter/material.dart';
3+
4+
final class CustomColorSchema {
5+
const CustomColorSchema(this.value);
6+
7+
final double value;
8+
9+
double get _v => value.clamp(-1.0, 1.0);
10+
11+
Color get seedColor {
12+
final v = _v;
13+
14+
// v < 0: 寒色 -> ニュートラル
15+
if (v < 0) {
16+
final t = v + 1.0;
17+
return _lerpStops(const [Colors.cyan, Colors.purple, Colors.grey], t);
18+
}
19+
20+
// v >= 0: ニュートラル -> 暖色
21+
final t = v;
22+
return _lerpStops(const [
23+
Colors.grey,
24+
Colors.yellow,
25+
Colors.deepOrange,
26+
Colors.pink,
27+
], t);
28+
}
29+
30+
ColorScheme toColorScheme({Brightness brightness = Brightness.light}) {
31+
print("seedColor: ${seedColor.toARGB32().toRadixString(16)}");
32+
if (seedColor == Color(0xFF9E9E9E)) {
33+
return grayColorScheme(brightness: brightness);
34+
}
35+
return ColorScheme.fromSeed(seedColor: seedColor, brightness: brightness);
36+
}
37+
38+
static Color _lerpStops(List<Color> colors, double t) {
39+
assert(colors.length >= 2);
40+
final clampedT = t.clamp(0.0, 1.0);
41+
final scaled = clampedT * (colors.length - 1);
42+
final index = scaled.floor();
43+
44+
if (index >= colors.length - 1) {
45+
return colors.last;
46+
}
47+
48+
final localT = scaled - index;
49+
return Color.lerp(colors[index], colors[index + 1], localT)!;
50+
}
51+
52+
static ColorScheme schemeFor(
53+
double value, {
54+
Brightness brightness = Brightness.light,
55+
}) {
56+
return CustomColorSchema(value).toColorScheme(brightness: brightness);
57+
}
58+
59+
ColorScheme grayColorScheme({Brightness brightness = Brightness.light}) {
60+
return grayLightColorScheme;
61+
}
62+
}
63+
64+
final ColorScheme grayLightColorScheme = ColorScheme(
65+
brightness: Brightness.light,
66+
primary: Colors.grey.shade900,
67+
onPrimary: Colors.white,
68+
primaryContainer: Colors.grey.shade200,
69+
onPrimaryContainer: Colors.grey.shade900,
70+
secondary: Colors.grey.shade800,
71+
onSecondary: Colors.white,
72+
secondaryContainer: Colors.grey.shade100,
73+
onSecondaryContainer: Colors.grey.shade900,
74+
tertiary: Colors.grey.shade700,
75+
onTertiary: Colors.white,
76+
tertiaryContainer: Colors.grey.shade100,
77+
onTertiaryContainer: Colors.grey.shade900,
78+
error: Colors.red.shade700,
79+
onError: Colors.white,
80+
errorContainer: Colors.red.shade100,
81+
onErrorContainer: Colors.red.shade900,
82+
surface: Colors.grey.shade50,
83+
onSurface: Colors.grey.shade900,
84+
surfaceContainerHighest: Colors.grey.shade100,
85+
onSurfaceVariant: Colors.grey.shade700,
86+
outline: Colors.grey.shade400,
87+
outlineVariant: Colors.grey.shade200,
88+
shadow: Colors.black,
89+
scrim: Colors.black,
90+
inverseSurface: Colors.grey.shade900,
91+
onInverseSurface: Colors.grey.shade50,
92+
inversePrimary: Colors.grey.shade200,
93+
);

0 commit comments

Comments
 (0)