File: palette_interpolation_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 15,471 bytes parent folder | download | duplicates (5)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/fonts/palette_interpolation.h"

#include "base/files/file_path.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/task_environment.h"
#include "third_party/blink/public/platform/file_path_conversion.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/testing/font_test_base.h"
#include "third_party/blink/renderer/platform/testing/font_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTypeface.h"

#include <utility>
#include <vector>

namespace {

constexpr double kMaxAlphaDifference = 0.01;

String pathToColorPalettesTestFont() {
  base::FilePath wpt_palette_font_path(
      blink::StringToFilePath(blink::test::BlinkWebTestsDir()));
  wpt_palette_font_path = wpt_palette_font_path.Append(FILE_PATH_LITERAL(
      "external/wpt/css/css-fonts/resources/COLR-palettes-test-font.ttf"));
  return blink::FilePathToString(wpt_palette_font_path);
}
String pathToNonColorTestFont() {
  return blink::test::BlinkWebTestsFontsTestDataPath("Ahem.ttf");
}

}  // namespace

namespace blink {

class PaletteInterpolationTest : public FontTestBase {
 protected:
  void SetUp() override {
    FontDescription::VariantLigatures ligatures;

    Font* color_palette_font = blink::test::CreateTestFont(
        AtomicString("Ahem"), pathToColorPalettesTestFont(), 16, &ligatures);
    color_palette_typeface_ =
        sk_ref_sp(color_palette_font->PrimaryFont()->PlatformData().Typeface());

    Font* non_color_font = blink::test::CreateTestFont(
        AtomicString("Ahem"), pathToNonColorTestFont(), 16, &ligatures);
    non_color_ahem_typeface_ =
        sk_ref_sp(non_color_font->PrimaryFont()->PlatformData().Typeface());
  }

  void ExpectColorsEqualInSRGB(
      Vector<FontPalette::FontPaletteOverride> overrides1,
      Vector<FontPalette::FontPaletteOverride> overrides2) {
    EXPECT_EQ(overrides1.size(), overrides2.size());
    for (wtf_size_t i = 0; i < overrides1.size(); i++) {
      EXPECT_EQ(overrides1[i].index, overrides2[i].index);
      Color color1 = overrides1[i].color;
      Color color2 = overrides2[i].color;
      EXPECT_EQ(DifferenceSquared(color1, color2), 0);
      // Due to the conversion from oklab to SRGB we should use epsilon
      // comparison.
      EXPECT_TRUE(std::fabs(color1.Alpha() - color2.Alpha()) <
                  kMaxAlphaDifference);
    }
  }

  sk_sp<SkTypeface> color_palette_typeface_;
  sk_sp<SkTypeface> non_color_ahem_typeface_;
};

TEST_F(PaletteInterpolationTest, RetrievePaletteIndexFromColorFont) {
  PaletteInterpolation palette_interpolation(color_palette_typeface_);
  scoped_refptr<FontPalette> palette =
      FontPalette::Create(FontPalette::kDarkPalette);
  std::optional<uint16_t> index =
      palette_interpolation.RetrievePaletteIndex(palette.get());
  EXPECT_TRUE(index.has_value());
  EXPECT_EQ(*index, 3);
}

TEST_F(PaletteInterpolationTest, RetrievePaletteIndexFromNonColorFont) {
  PaletteInterpolation palette_interpolation(non_color_ahem_typeface_);
  scoped_refptr<FontPalette> palette =
      FontPalette::Create(FontPalette::kLightPalette);
  std::optional<uint16_t> index =
      palette_interpolation.RetrievePaletteIndex(palette.get());
  EXPECT_FALSE(index.has_value());
}

TEST_F(PaletteInterpolationTest, MixCustomPalettesAtHalfTime) {
  PaletteInterpolation palette_interpolation(color_palette_typeface_);
  scoped_refptr<FontPalette> palette_start =
      FontPalette::Create(AtomicString("palette1"));
  palette_start->SetBasePalette({FontPalette::kIndexBasePalette, 3});
  // palette_start has the following list of color records:
  // { rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%),
  //   rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%) }

  scoped_refptr<FontPalette> palette_end =
      FontPalette::Create(AtomicString("palette2"));
  palette_end->SetBasePalette({FontPalette::kIndexBasePalette, 7});
  // palette_end has the following list of color records:
  // { rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%),
  //   rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%) }

  scoped_refptr<FontPalette> palette =
      FontPalette::Mix(palette_start, palette_end, 50, 50, 0.5, 1.0,
                       Color::ColorSpace::kOklab, std::nullopt);
  Vector<FontPalette::FontPaletteOverride> actual_color_records =
      palette_interpolation.ComputeInterpolableFontPalette(palette.get());
  // We expect each color to be half-way between palette_start and palette_end
  // after interpolation in the Oklab interpolation color space and conversion
  // back to sRGB.
  Vector<FontPalette::FontPaletteOverride> expected_color_records = {
      {0, Color::FromRGBA(254, 255, 172, 255)},
      {1, Color::FromRGBA(0, 0, 99, 255)},
      {2, Color::FromRGBA(253, 45, 155, 255)},
      {3, Color::FromRGBA(0, 255, 169, 255)},
      {4, Color::FromRGBA(254, 255, 172, 255)},
      {5, Color::FromRGBA(0, 0, 99, 255)},
      {6, Color::FromRGBA(253, 45, 155, 255)},
      {7, Color::FromRGBA(0, 255, 169, 255)},
  };
  ExpectColorsEqualInSRGB(actual_color_records, expected_color_records);
}

TEST_F(PaletteInterpolationTest, MixCustomAndNonExistingPalettes) {
  PaletteInterpolation palette_interpolation(color_palette_typeface_);
  scoped_refptr<FontPalette> palette_start =
      FontPalette::Create(AtomicString("palette1"));
  palette_start->SetBasePalette({FontPalette::kIndexBasePalette, 3});
  // palette_start has the following list of color records:
  // { rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%),
  //   rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%) }

  scoped_refptr<FontPalette> palette_end =
      FontPalette::Create(AtomicString("palette2"));
  palette_end->SetBasePalette({FontPalette::kIndexBasePalette, 16});
  // Palette under index 16 does not exist, so instead normal palette is used.
  // Normal palette has the following list of color records:
  // { rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%),
  //   rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%),
  //   rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%) }

  scoped_refptr<FontPalette> palette =
      FontPalette::Mix(palette_start, palette_end, 50, 50, 0.5, 1.0,
                       Color::ColorSpace::kOklab, std::nullopt);
  Vector<FontPalette::FontPaletteOverride> actual_color_records =
      palette_interpolation.ComputeInterpolableFontPalette(palette.get());
  // We expect each color to be half-way between palette_start and normal
  // palette after interpolation in the Oklab interpolation color space and
  // conversion back to sRGB.
  Vector<FontPalette::FontPaletteOverride> expected_color_records = {
      {0, Color::FromRGBA(99, 99, 0, 255)},
      {1, Color::FromRGBA(140, 83, 162, 255)},
      {2, Color::FromRGBA(198, 180, 180, 255)},
      {3, Color::FromRGBA(176, 255, 176, 255)},
      {4, Color::FromRGBA(116, 163, 255, 255)},
      {5, Color::FromRGBA(99, 0, 99, 255)},
      {6, Color::FromRGBA(210, 169, 148, 255)},
      {7, Color::FromRGBA(173, 255, 166, 255)},
  };
  ExpectColorsEqualInSRGB(actual_color_records, expected_color_records);
}

TEST_F(PaletteInterpolationTest, MixNonExistingPalettes) {
  PaletteInterpolation palette_interpolation(color_palette_typeface_);
  scoped_refptr<FontPalette> palette_start =
      FontPalette::Create(AtomicString("palette1"));
  // Palette under index 16 does not exist, so instead normal palette is used.
  palette_start->SetBasePalette({FontPalette::kIndexBasePalette, 16});

  scoped_refptr<FontPalette> palette_end =
      FontPalette::Create(AtomicString("palette2"));
  // Palette under index 17 does not exist, so instead normal palette is used.
  palette_end->SetBasePalette({FontPalette::kIndexBasePalette, 17});

  scoped_refptr<FontPalette> palette =
      FontPalette::Mix(palette_start, palette_end, 50, 50, 0.5, 1.0,
                       Color::ColorSpace::kOklab, std::nullopt);
  Vector<FontPalette::FontPaletteOverride> actual_color_records =
      palette_interpolation.ComputeInterpolableFontPalette(palette.get());
  // Since both of the endpoints are equal and have color records from normal
  // palette, we expect each colors from the normal palette in the result list.
  Vector<FontPalette::FontPaletteOverride> expected_color_records = {
      {0, Color::FromRGBA(0, 0, 0, 255)},
      {1, Color::FromRGBA(255, 0, 0, 255)},
      {2, Color::FromRGBA(0, 255, 0, 255)},
      {3, Color::FromRGBA(255, 255, 0, 255)},
      {4, Color::FromRGBA(0, 0, 255, 255)},
      {5, Color::FromRGBA(255, 0, 255, 255)},
      {6, Color::FromRGBA(0, 255, 255, 255)},
      {7, Color::FromRGBA(255, 255, 255, 255)},
  };
  ExpectColorsEqualInSRGB(actual_color_records, expected_color_records);
}

TEST_F(PaletteInterpolationTest, MixCustomPalettesInOklab) {
  PaletteInterpolation palette_interpolation(color_palette_typeface_);
  scoped_refptr<FontPalette> palette_start =
      FontPalette::Create(AtomicString("palette1"));
  palette_start->SetBasePalette({FontPalette::kIndexBasePalette, 3});
  // palette_start has the following list of color records:
  // { rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%),
  //   rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%) }

  scoped_refptr<FontPalette> palette_end =
      FontPalette::Create(AtomicString("palette2"));
  palette_end->SetBasePalette({FontPalette::kIndexBasePalette, 7});
  // palette_end has the following list of color records:
  // { rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%),
  //   rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%) }

  scoped_refptr<FontPalette> palette =
      FontPalette::Mix(palette_start, palette_end, 70, 30, 0.3, 1.0,
                       Color::ColorSpace::kOklab, std::nullopt);
  Vector<FontPalette::FontPaletteOverride> actual_color_records =
      palette_interpolation.ComputeInterpolableFontPalette(palette.get());
  // We expect each color to be equal palette_start * 0.7 + palette_end * 0.3
  // after interpolation in the sRGB interpolation color space.
  Vector<FontPalette::FontPaletteOverride> expected_color_records = {
      {0, Color::FromRGBA(254, 255, 131, 255)},
      {1, Color::FromRGBA(0, 0, 158, 255)},
      {2, Color::FromRGBA(254, 42, 196, 255)},
      {3, Color::FromRGBA(0, 255, 205, 255)},
      {4, Color::FromRGBA(254, 255, 207, 255)},
      {5, Color::FromRGBA(0, 0, 46, 255)},
      {6, Color::FromRGBA(254, 39, 112, 255)},
      {7, Color::FromRGBA(0, 255, 128, 255)},
  };
  ExpectColorsEqualInSRGB(actual_color_records, expected_color_records);
}

TEST_F(PaletteInterpolationTest, MixCustomPalettesInSRGB) {
  PaletteInterpolation palette_interpolation(color_palette_typeface_);
  scoped_refptr<FontPalette> palette_start =
      FontPalette::Create(AtomicString("palette1"));
  palette_start->SetBasePalette({FontPalette::kIndexBasePalette, 3});
  // palette_start has the following list of color records:
  // { rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%),
  //   rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%) }

  scoped_refptr<FontPalette> palette_end =
      FontPalette::Create(AtomicString("palette2"));
  palette_end->SetBasePalette({FontPalette::kIndexBasePalette, 7});
  // palette_end has the following list of color records:
  // { rgba(255, 255, 255, 255) = oklab(100%, 0%, 0%),
  //   rgba(0, 0, 0, 255) = oklab(0%, 0%, 0%),
  //   rgba(255, 0, 0, 255) = oklab(62.8%, 56.25%, 31.5%),
  //   rgba(0, 255, 0, 255) = oklab(86.6%, -58.5%, 44.75%),
  //   rgba(255, 255, 0, 255) = oklab(96.8%, -17.75%, 49.75%),
  //   rgba(0, 0, 255, 255) = oklab(45.2%, -8%, -78%),
  //   rgba(255, 0, 255, 255) = oklab(70.2%, 68.75%, -42.25%),
  //   rgba(0, 255, 255, 255) = oklab(90.5%, -37.25%, -9.75%) }

  scoped_refptr<FontPalette> palette =
      FontPalette::Mix(palette_start, palette_end, 70, 30, 0.3, 1.0,
                       Color::ColorSpace::kSRGB, std::nullopt);
  Vector<FontPalette::FontPaletteOverride> actual_color_records =
      palette_interpolation.ComputeInterpolableFontPalette(palette.get());
  // We expect each color to be equal palette_start * 0.7 + palette_end * 0.3
  // after interpolation in the Oklab interpolation color space and conversion
  // back to sRGB.
  Vector<FontPalette::FontPaletteOverride> expected_color_records = {
      {0, Color::FromRGBA(255, 255, 77, 255)},
      {1, Color::FromRGBA(0, 0, 179, 255)},
      {2, Color::FromRGBA(255, 0, 179, 255)},
      {3, Color::FromRGBA(0, 255, 179, 255)},
      {4, Color::FromRGBA(255, 255, 179, 255)},
      {5, Color::FromRGBA(0, 0, 77, 255)},
      {6, Color::FromRGBA(255, 0, 77, 255)},
      {7, Color::FromRGBA(0, 255, 77, 255)},
  };
  ExpectColorsEqualInSRGB(actual_color_records, expected_color_records);
}

}  // namespace blink