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 | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame] | 15 | REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, 255*pmf.a())); |
| 16 | REPORTER_ASSERT(r, SkScalarNearlyEqual(204.0f, 255*pmf.r())); |
| 17 | REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, 255*pmf.g())); |
| 18 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 51.0f, 255*pmf.b())); |
mtklein | 3d62683 | 2015-04-03 07:05:20 -0700 | [diff] [blame] | 19 | REPORTER_ASSERT(r, c == pmf.round()); |
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. |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame] | 22 | pmf = SkPMFloat(254.5f/255, 203.5f/255, 153.1f/255, 50.8f/255); |
mtklein | 3d62683 | 2015-04-03 07:05:20 -0700 | [diff] [blame] | 23 | REPORTER_ASSERT(r, c == pmf.round()); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 24 | |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame] | 25 | SkPMFloat clamped(SkPMFloat(510.0f/255, 153.0f/255, 1.0f/255, -0.2f/255).round()); |
| 26 | REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, 255*clamped.a())); |
| 27 | REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, 255*clamped.r())); |
| 28 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 1.0f, 255*clamped.g())); |
| 29 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.0f, 255*clamped.b())); |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 30 | |
mtklein | 0340df5 | 2015-03-31 08:17:00 -0700 | [diff] [blame] | 31 | // Test SkPMFloat <-> Sk4f conversion. |
| 32 | Sk4f fs = clamped; |
| 33 | SkPMFloat scaled = fs * Sk4f(0.25f); |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame] | 34 | REPORTER_ASSERT(r, SkScalarNearlyEqual(63.75f, 255*scaled.a())); |
| 35 | REPORTER_ASSERT(r, SkScalarNearlyEqual(38.25f, 255*scaled.r())); |
| 36 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.25f, 255*scaled.g())); |
| 37 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.00f, 255*scaled.b())); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 38 | } |