Mike Klein | 7a0ba1c | 2017-07-25 17:26:19 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | DEF_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 Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 33 | sk_sp<SkColorFilter> composed = table->makeComposed(to_srgb); |
Mike Klein | 7a0ba1c | 2017-07-25 17:26:19 -0400 | [diff] [blame] | 34 | for (auto color : colors) { |
| 35 | REPORTER_ASSERT(r, composed->filterColor(color) == color); |
| 36 | } |
| 37 | } |