reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "SkColor.h" |
reed | fac6845 | 2016-01-23 10:15:39 -0800 | [diff] [blame] | 9 | #include "SkShader.h" |
reed | 5f34e8e | 2016-01-23 14:19:06 -0800 | [diff] [blame^] | 10 | #include "SkColorMatrixFilter.h" |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 11 | #include "Test.h" |
| 12 | #include "SkRandom.h" |
| 13 | |
| 14 | DEF_TEST(SkColor4f_FromColor, reporter) { |
| 15 | const struct { |
| 16 | SkColor fC; |
| 17 | SkColor4f fC4; |
| 18 | } recs[] = { |
| 19 | { SK_ColorBLACK, { 1, 0, 0, 0 } }, |
| 20 | { SK_ColorWHITE, { 1, 1, 1, 1 } }, |
| 21 | { SK_ColorRED, { 1, 1, 0, 0 } }, |
| 22 | { SK_ColorGREEN, { 1, 0, 1, 0 } }, |
| 23 | { SK_ColorBLUE, { 1, 0, 0, 1 } }, |
| 24 | { 0, { 0, 0, 0, 0 } }, |
| 25 | { 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } }, |
| 26 | }; |
| 27 | |
| 28 | for (const auto& r : recs) { |
| 29 | SkColor4f c4 = SkColor4f::FromColor(r.fC); |
| 30 | REPORTER_ASSERT(reporter, c4 == r.fC4); |
| 31 | } |
| 32 | } |
| 33 | |
reed | 5f34e8e | 2016-01-23 14:19:06 -0800 | [diff] [blame^] | 34 | static bool nearly_equal(float a, float b) { |
| 35 | const float kTolerance = 1.0f / (1 << 20); |
| 36 | return fabsf(a - b) < kTolerance; |
| 37 | } |
| 38 | |
| 39 | DEF_TEST(SkColor4f_premul, reporter) { |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 40 | SkRandom rand; |
| 41 | |
| 42 | for (int i = 0; i < 1000000; ++i) { |
| 43 | // First just test opaque colors, so that the premul should be exact |
| 44 | SkColor4f c4 { |
| 45 | 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1() |
| 46 | }; |
| 47 | SkPM4f pm4 = c4.premul(); |
| 48 | REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); |
| 49 | REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR); |
| 50 | REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG); |
| 51 | REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB); |
| 52 | |
| 53 | // We compare with a tolerance, in case our premul multiply is implemented at slightly |
| 54 | // different precision than the test code. |
| 55 | c4.fA = rand.nextUScalar1(); |
| 56 | pm4 = c4.premul(); |
| 57 | REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); |
| 58 | REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_R_INDEX], c4.fA * c4.fR)); |
| 59 | REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_G_INDEX], c4.fA * c4.fG)); |
| 60 | REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_B_INDEX], c4.fA * c4.fB)); |
| 61 | } |
| 62 | } |