/
gamut_map_method.dart
65 lines (56 loc) · 2.38 KB
/
gamut_map_method.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
// Copyright 2024 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:meta/meta.dart';
import '../../exception.dart';
import '../color.dart';
import 'gamut_map_method/clip.dart';
import 'gamut_map_method/local_minde.dart';
/// Different algorithms that can be used to map an out-of-gamut Sass color into
/// the gamut for its color space.
///
/// {@category Value}
@sealed
abstract base class GamutMapMethod {
/// Clamp each color channel that's outside the gamut to the minimum or
/// maximum value for that channel.
///
/// This algorithm will produce poor visual results, but it may be useful to
/// match the behavior of other situations in which a color can be clipped.
static const GamutMapMethod clip = ClipGamutMap();
/// The algorithm specified in [the original Color Level 4 candidate
/// recommendation].
///
/// This maps in the Oklch color space, using the [deltaEOK] color difference
/// formula and the [local-MINDE] improvement.
///
/// [the original Color Level 4 candidate recommendation]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#css-gamut-mapping
/// [deltaEOK]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#color-difference-OK
/// [local-MINDE]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#GM-chroma-local-MINDE
static const GamutMapMethod localMinde = LocalMindeGamutMap();
/// The Sass name of the gamut-mapping algorithm.
final String name;
/// @nodoc
@internal
const GamutMapMethod(this.name);
/// Parses a [GamutMapMethod] from its Sass name.
///
/// Throws a [SassScriptException] if there is no method with the given
/// [name]. If this came from a function argument, [argumentName] is the
/// argument name (without the `$`). This is used for error reporting.
factory GamutMapMethod.fromName(String name, [String? argumentName]) =>
switch (name) {
'clip' => GamutMapMethod.clip,
'local-minde' => GamutMapMethod.localMinde,
_ => throw SassScriptException(
'Unknown gamut map method "$name".', argumentName)
};
/// Maps [color] to its gamut using this method's algorithm.
///
/// Callers should use [SassColor.toGamut] instead of this method.
///
/// @nodoc
@internal
SassColor map(SassColor color);
String toString() => name;
}