mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 1 | #include "SkPMFloat.h" |
| 2 | #include "Test.h" |
| 3 | |
| 4 | DEF_TEST(SkPMFloat, r) { |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 5 | // Test SkPMColor <-> SkPMFloat |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 6 | SkPMColor c = SkPreMultiplyColor(0xFFCC9933); |
| 7 | |
| 8 | SkPMFloat pmf; |
| 9 | pmf.set(c); |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 10 | REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, pmf.a())); |
| 11 | REPORTER_ASSERT(r, SkScalarNearlyEqual(204.0f, pmf.r())); |
| 12 | REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, pmf.g())); |
| 13 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 51.0f, pmf.b())); |
mtklein | 0aebf5d | 2015-03-03 08:57:07 -0800 | [diff] [blame] | 14 | REPORTER_ASSERT(r, c == pmf.get()); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 15 | |
mtklein | 0aebf5d | 2015-03-03 08:57:07 -0800 | [diff] [blame] | 16 | // Test rounding. (Don't bother testing .5... we don't care which way it goes.) |
| 17 | pmf.setA(254.6f); |
| 18 | pmf.setR(204.3f); |
| 19 | pmf.setG(153.1f); |
| 20 | pmf.setB( 50.8f); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 21 | REPORTER_ASSERT(r, c == pmf.get()); |
| 22 | |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 23 | // Test clamping. |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 24 | SkPMFloat unclamped; |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 25 | unclamped.setA(+510.0f); |
| 26 | unclamped.setR(+153.0f); |
| 27 | unclamped.setG( +1.0f); |
| 28 | unclamped.setB( -0.2f); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 29 | |
| 30 | SkPMFloat clamped; |
| 31 | clamped.set(unclamped.clamped()); |
| 32 | |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 33 | REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, clamped.a())); |
| 34 | REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, clamped.r())); |
| 35 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 1.0f, clamped.g())); |
| 36 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.0f, clamped.b())); |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 37 | |
| 38 | // Test SkPMFloat <-> Sk4f conversion. |
| 39 | Sk4f fs = clamped; |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 40 | SkPMFloat scaled = fs.multiply(0.25f); |
mtklein | 870b9ea | 2015-02-26 10:43:16 -0800 | [diff] [blame] | 41 | |
mtklein | 60d2a32 | 2015-03-03 07:46:15 -0800 | [diff] [blame] | 42 | REPORTER_ASSERT(r, SkScalarNearlyEqual(63.75f, scaled.a())); |
| 43 | REPORTER_ASSERT(r, SkScalarNearlyEqual(38.25f, scaled.r())); |
| 44 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.25f, scaled.g())); |
| 45 | REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.00f, scaled.b())); |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 46 | } |