blob: 857aa7ed6994df6aba0d209b066d17ccb55c7c93 [file] [log] [blame]
Mike Klein7a0ba1c2017-07-25 17:26:19 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9
10#include "SkColorSpace.h"
11#include "SkTableColorFilter.h"
12#include "SkToSRGBColorFilter.h"
13
14// SkToSRGBColorFilter makes it easy to create out of range (>1, <0) color values.
15// Those can be dangerous as inputs to naive implementation of SkTableColorFilter.
16// This tests that our implementation is safe.
17
18DEF_TEST(TableColorFilter, r) {
19 // Using a wide source gamut will make saturated colors go well out of range of sRGB.
20 auto rec2020 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
21 SkColorSpace::kRec2020_Gamut);
22 sk_sp<SkColorFilter> to_srgb = SkToSRGBColorFilter::Make(rec2020);
23
24 // Any table will work fine here. An identity table makes testing easy.
25 uint8_t identity[256];
26 for (int i = 0; i < 256; i++) {
27 identity[i] = i;
28 }
29 sk_sp<SkColorFilter> table = SkTableColorFilter::Make(identity);
30
31 // The rec2020 primaries are not representable in sRGB, but will clamp to the sRGB primaries.
32 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
Mike Reed19d7bd62018-02-19 14:10:57 -050033 sk_sp<SkColorFilter> composed = table->makeComposed(to_srgb);
Mike Klein7a0ba1c2017-07-25 17:26:19 -040034 for (auto color : colors) {
35 REPORTER_ASSERT(r, composed->filterColor(color) == color);
36 }
37}