blob: fbd8626cc813bff944449373d12d31d76fee1e24 [file] [log] [blame]
mtkleina2f4be72015-02-23 10:04:34 -08001#include "SkPMFloat.h"
2#include "Test.h"
3
4DEF_TEST(SkPMFloat, r) {
mtklein870b9ea2015-02-26 10:43:16 -08005 // Test SkPMColor <-> SkPMFloat
mtkleina2f4be72015-02-23 10:04:34 -08006 SkPMColor c = SkPreMultiplyColor(0xFFCC9933);
7
8 SkPMFloat pmf;
9 pmf.set(c);
mtklein60d2a322015-03-03 07:46:15 -080010 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()));
mtklein0aebf5d2015-03-03 08:57:07 -080014 REPORTER_ASSERT(r, c == pmf.get());
mtkleina2f4be72015-02-23 10:04:34 -080015
mtklein0aebf5d2015-03-03 08:57:07 -080016 // 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);
mtkleina2f4be72015-02-23 10:04:34 -080021 REPORTER_ASSERT(r, c == pmf.get());
22
mtklein870b9ea2015-02-26 10:43:16 -080023 // Test clamping.
mtkleina2f4be72015-02-23 10:04:34 -080024 SkPMFloat unclamped;
mtklein60d2a322015-03-03 07:46:15 -080025 unclamped.setA(+510.0f);
26 unclamped.setR(+153.0f);
27 unclamped.setG( +1.0f);
28 unclamped.setB( -0.2f);
mtkleina2f4be72015-02-23 10:04:34 -080029
30 SkPMFloat clamped;
31 clamped.set(unclamped.clamped());
32
mtklein60d2a322015-03-03 07:46:15 -080033 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()));
mtklein870b9ea2015-02-26 10:43:16 -080037
38 // Test SkPMFloat <-> Sk4f conversion.
39 Sk4f fs = clamped;
mtklein60d2a322015-03-03 07:46:15 -080040 SkPMFloat scaled = fs.multiply(0.25f);
mtklein870b9ea2015-02-26 10:43:16 -080041
mtklein60d2a322015-03-03 07:46:15 -080042 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()));
mtkleina2f4be72015-02-23 10:04:34 -080046}