mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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 | |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 8 | #include "SkPMFloat.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | DEF_TEST(SkPMFloat, r) { |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 12 | // Test SkPMColor <-> SkPMFloat |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 13 | SkPMColor c = SkPreMultiplyColor(0xFFCC9933); |
mtklein | 4e644f5 | 2015-03-04 11:25:27 -0800 | [diff] [blame] | 14 | SkPMFloat pmf(c); |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 15 | REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, pmf.a())); |
| 16 | REPORTER_ASSERT(r, SkScalarNearlyEqual(204.0f, pmf.r())); |
| 17 | REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, pmf.g())); |
| 18 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 51.0f, pmf.b())); |
mtklein | 0aebf5d | 2015-03-03 08:57:07 -0800 | [diff] [blame] | 19 | REPORTER_ASSERT(r, c == pmf.get()); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 20 | |
mtklein | 92d04da | 2015-03-23 12:01:45 -0700 | [diff] [blame] | 21 | // Test rounding. |
| 22 | pmf = SkPMFloat(254.5f, 203.5f, 153.1f, 50.8f); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 23 | REPORTER_ASSERT(r, c == pmf.get()); |
| 24 | |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 25 | // Test clamping. |
mtklein | 4e644f5 | 2015-03-04 11:25:27 -0800 | [diff] [blame] | 26 | SkPMFloat clamped(SkPMFloat(510.0f, 153.0f, 1.0f, -0.2f).clamped()); |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 27 | REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, clamped.a())); |
| 28 | REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, clamped.r())); |
| 29 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 1.0f, clamped.g())); |
| 30 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.0f, clamped.b())); |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 31 | |
| 32 | // Test SkPMFloat <-> Sk4f conversion. |
| 33 | Sk4f fs = clamped; |
mtklein | 26bf90e | 2015-03-20 06:00:57 -0700 | [diff] [blame] | 34 | SkPMFloat scaled = fs * Sk4f(0.25f); |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 35 | REPORTER_ASSERT(r, SkScalarNearlyEqual(63.75f, scaled.a())); |
| 36 | REPORTER_ASSERT(r, SkScalarNearlyEqual(38.25f, scaled.r())); |
| 37 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.25f, scaled.g())); |
| 38 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.00f, scaled.b())); |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 39 | |
| 40 | // Test 4-at-a-time conversions. |
| 41 | SkPMColor colors[4] = { 0xFF000000, 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }; |
| 42 | SkPMFloat floats[4]; |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame^] | 43 | SkPMFloat::From4PMColors(colors, floats+0, floats+1, floats+2, floats+3); |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 44 | |
| 45 | SkPMColor back[4]; |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame^] | 46 | SkPMFloat::To4PMColors(floats[0], floats[1], floats[2], floats[3], back); |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 47 | for (int i = 0; i < 4; i++) { |
| 48 | REPORTER_ASSERT(r, back[i] == colors[i]); |
| 49 | } |
| 50 | |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame^] | 51 | SkPMFloat::ClampTo4PMColors(floats[0], floats[1], floats[2], floats[3], back); |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 52 | for (int i = 0; i < 4; i++) { |
| 53 | REPORTER_ASSERT(r, back[i] == colors[i]); |
| 54 | } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 55 | } |