Colour vision: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: roles (line 15), paint (line 109), table (line 127), lint (line 163), check (line 182).
1/// Four marks a player has to tell apart, in the colours their roles give
2/// them, drawn as someone missing a cone sees them or corrected for them, and
3/// the same four in the colours a player chose instead.
4///
5/// Quoted by `colour_vision.md` and shown whole in the Source tab.
6library;
8import 'dart:math' as math;
9import 'dart:ui' show Color;
11import 'package:flutter3d_game/flutter3d_game.dart';
12import 'package:flutter3d_showcase/pages/post/post_stage.dart';
13import 'package:flutter3d_showcase/src/demo/demo.dart';
15/// What the colours on this page mean. A HUD asks for "danger", never for
16/// red, so the player can move one of them without touching the others.
17final ColorRoles _roles = ColorRoles(const <ColorRole>[
18 ColorRole('danger', 'Danger', Color(0xFFD03A2C)),
19 ColorRole('safe', 'Safe ground', Color(0xFF3C9A3A)),
20 ColorRole('loot', 'Loot', Color(0xFFE8C547)),
21 ColorRole('water', 'Water', Color(0xFF3A7BD5)),
22]);
24/// A player who picked from Okabe and Ito's eight in the Colours section:
25/// each number is the place in `ColorRoles.choices`, nought being the
26/// role's own colour. Vermillion, blue, yellow and sky blue.
27GameSettings _chosen() => const GameSettings()
28 .withValue(GameSettingKeys.colorRole('danger'), 7)
29 .withValue(GameSettingKeys.colorRole('safe'), 6)
30 .withValue(GameSettingKeys.colorRole('loot'), 5)
31 .withValue(GameSettingKeys.colorRole('water'), 3);
33/// The picture the page draws: as it is, as each deficiency sees it, or
34/// corrected for each.
35const List<String> _pictures = <String>[
36 'As drawn',
37 'Seen protan',
38 'Seen deutan',
39 'Seen tritan',
40 'Corrected for protan',
41 'Corrected for deutan',
42 'Corrected for tritan',
43];
45/// The `colour-vision` page: four role-coloured marks drawn as a colour-
46/// blind player sees them, corrected, or in colours the player chose. Its
47/// check is that the lint catches red against green for a deutan, and that
48/// to a deutan the player's colours stand three times further apart than the
49/// defaults and the correction moves them apart too.
50final class ColourVisionDemo extends ShowcaseDemo {
51 /// Which picture is drawn: 0 as drawn, 1 to 3 seen by a protan, deutan or
52 /// tritan, 4 to 6 corrected for them.
53 int picture = 2;
55 /// Whether the marks wear the colours the player chose rather than their
56 /// roles' defaults.
57 bool playerColours = false;
59 final GameSettings _defaults = const GameSettings();
60 final GameSettings _player = _chosen();
61 final List<(ColorRole, RenderMaterial)> _marks =
62 <(ColorRole, RenderMaterial)>[];
63 final Map<int, TextureHandle?> _tables = <int, TextureHandle?>{};
64 late final GraphicsDevice _device;
65 late LookSettings _look;
68 void configureView(DemoContext context) => PostStage.frame(context);
71 Scene build(DemoContext context) {
72 _device = context.device;
73 final Scene scene = Scene()
74 ..ambientIntensity = 0.45 * LightNode.legacyUnit
75 ..add(
76 MeshNode(
77 DeviceMesh.upload(
78 context.device,
79 const PlaneShape(width: 10, depth: 6).build(),
80 ),
81 RenderMaterial(
82 name: 'floor',
83 baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.52, 1.0),
84 ),
85 name: 'floor',
86 ),
87 )
88 ..add(
89 LightNode(name: 'sun', intensity: 2.5 * LightNode.legacyUnit)
90 ..setLocalForward(Vector3(-0.3, -0.9, -0.4)),
91 );
92 final DeviceMesh slab = DeviceMesh.upload(
93 context.device,
94 CuboidShape(size: Vector3(1.4, 1.4, 0.4)).build(),
95 );
96 for (var i = 0; i < _roles.roles.length; i++) {
97 final ColorRole role = _roles.roles[i];
98 final material = RenderMaterial(name: role.name, roughness: 0.8);
99 _marks.add((role, material));
100 scene.add(
101 MeshNode(slab, material, name: role.name)
102 ..setPosition(-2.7 + i * 1.8, 0.7, 0.0),
103 );
104 }
105 _paint();
106 return scene;
107 }
109 /// Each mark in the colour its role has for whoever is playing. A swatch
110 /// is a display colour, so it is taken to linear light for the material.
111 void _paint() {
112 final GameSettings config = playerColours ? _player : _defaults;
113 for (final (ColorRole role, RenderMaterial material) in _marks) {
114 final Color c = _roles.of(role, config);
115 material.baseColor = LinearColor.fromSrgb(
116 _linear(c.r),
117 _linear(c.g),
118 _linear(c.b),
119 1,
120 );
121 }
122 }
124 static double _linear(double c) =>
125 c < 0.04045 ? c / 12.92 : math.pow((c + 0.055) / 1.055, 2.4).toDouble();
127 /// The colour table for [picture]: a matrix baked into the strip the
128 /// composite reads after the tone map, made once per choice.
129 TextureHandle? _table(int picture) => _tables.putIfAbsent(picture, () {
130 if (picture == 0) return null;
131 final ColorVisionDeficiency kind =
132 ColorVisionDeficiency.values[(picture - 1) % 3];
133 final ColorVision vision = picture <= 3
134 ? ColorVision.simulate(kind)
135 : ColorVision.correct(kind);
136 return vision.upload(_device);
137 });
140 RenderSettings settings(DemoContext context) {
141 _look = LookSettings(lut: _table(picture));
142 return RenderSettings(look: _look);
143 }
146 List<DemoControl> controls(DemoContext context) => <DemoControl>[
147 ChoiceControl(
148 'Picture',
149 options: _pictures,
150 index: () => picture,
151 onChanged: (int i) => picture = i,
152 ),
153 ToggleControl(
154 'The player\'s own colours',
155 value: () => playerColours,
156 onChanged: (bool v) {
157 playerColours = v;
158 _paint();
159 },
160 ),
161 ];
163 /// How far apart danger and safe ground look to someone missing the
164 /// middle cone, in CIE 1976 ΔE, in the colours [config] gives them —
165 /// corrected for that first when [corrected].
166 double _deutanGap(GameSettings config, {bool corrected = false}) {
167 const ColorVision seen = ColorVision.simulate(ColorVisionDeficiency.deutan);
168 const ColorVision fix = ColorVision.correct(ColorVisionDeficiency.deutan);
169 RgbTriple through(String name) {
170 final Color c = _roles.of(_roles.named(name)!, config);
171 final RgbTriple(:r, :g, :b) = corrected
172 ? fix.applyEncoded(c.r, c.g, c.b)
173 : RgbTriple(r: c.r, g: c.g, b: c.b);
174 return seen.applyEncoded(r, g, b);
175 }
177 return ColorVision.difference(through('danger'), through('safe'));
178 }
181 void verify(Scene scene, FrameResult frame) {
182 // The lint finds the pair a deutan runs together in the game's colours,
183 // and nothing at all in the colours the player chose.
184 final names = <String>['danger', 'safe', 'loot', 'water'];
185 final found = _roles.confusions(names, _defaults);
186 if (!found.any(
187 (c) =>
188 c.a == 'danger' &&
189 c.b == 'safe' &&
190 c.by == ColorVisionDeficiency.deutan,
191 )) {
192 throw StateError('the lint missed red against green for a deutan');
193 }
194 if (_roles.confusions(names, _player).isNotEmpty) {
195 throw StateError('the player\'s colours still run together');
196 }
197 // As a deutan sees them: the game's red and green are closer than the
198 // player's vermillion and blue, and closer than the correction leaves
199 // them.
200 final double asDrawn = _deutanGap(_defaults);
201 final double remapped = _deutanGap(_player);
202 final double corrected = _deutanGap(_defaults, corrected: true);
203 if (!(asDrawn < 20.0 && remapped > 3 * asDrawn && corrected > asDrawn)) {
204 throw StateError(
205 'deutan gaps: as drawn $asDrawn, remapped $remapped, '
206 'corrected $corrected',
207 );
208 }
209 if (_table(picture) != null && !_look.gradesThroughLut) {
210 throw StateError('the colour table would not be sampled this frame');
211 }
212 if (!passRan(frame, 'composite')) {
213 throw StateError('the composite pass, which reads the table, is absent');
214 }
215 if (frame.drawCalls < 1) throw StateError('nothing reached the frame');
216 }
217}