2015-03-25 20:43:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-02-23 18:04:34 +00:00
|
|
|
#include "SkPMFloat.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
DEF_TEST(SkPMFloat, r) {
|
2015-02-26 18:43:16 +00:00
|
|
|
// Test SkPMColor <-> SkPMFloat
|
2015-02-23 18:04:34 +00:00
|
|
|
SkPMColor c = SkPreMultiplyColor(0xFFCC9933);
|
2015-03-04 19:25:27 +00:00
|
|
|
SkPMFloat pmf(c);
|
2015-03-03 15:46:15 +00:00
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, pmf.a()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(204.0f, pmf.r()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, pmf.g()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual( 51.0f, pmf.b()));
|
2015-03-03 16:57:07 +00:00
|
|
|
REPORTER_ASSERT(r, c == pmf.get());
|
2015-02-23 18:04:34 +00:00
|
|
|
|
2015-03-23 19:01:45 +00:00
|
|
|
// Test rounding.
|
|
|
|
pmf = SkPMFloat(254.5f, 203.5f, 153.1f, 50.8f);
|
2015-02-23 18:04:34 +00:00
|
|
|
REPORTER_ASSERT(r, c == pmf.get());
|
|
|
|
|
2015-03-26 19:32:29 +00:00
|
|
|
pmf = SkPMFloat(255.9f, 204.01f, 153.0f, -0.9f);
|
|
|
|
REPORTER_ASSERT(r, SkPreMultiplyColor(0xFFCC9900) == pmf.trunc());
|
|
|
|
|
2015-02-26 18:43:16 +00:00
|
|
|
// Test clamping.
|
2015-03-04 19:25:27 +00:00
|
|
|
SkPMFloat clamped(SkPMFloat(510.0f, 153.0f, 1.0f, -0.2f).clamped());
|
2015-03-03 15:46:15 +00:00
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, clamped.a()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, clamped.r()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual( 1.0f, clamped.g()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.0f, clamped.b()));
|
2015-02-26 18:43:16 +00:00
|
|
|
|
2015-03-31 15:17:00 +00:00
|
|
|
// Test SkPMFloat <-> Sk4f conversion.
|
|
|
|
Sk4f fs = clamped;
|
|
|
|
SkPMFloat scaled = fs * Sk4f(0.25f);
|
2015-03-03 15:46:15 +00:00
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(63.75f, scaled.a()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual(38.25f, scaled.r()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.25f, scaled.g()));
|
|
|
|
REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.00f, scaled.b()));
|
2015-03-05 19:31:59 +00:00
|
|
|
|
|
|
|
// Test 4-at-a-time conversions.
|
|
|
|
SkPMColor colors[4] = { 0xFF000000, 0xFFFF0000, 0xFF00FF00, 0xFF0000FF };
|
|
|
|
SkPMFloat floats[4];
|
2015-03-25 20:43:34 +00:00
|
|
|
SkPMFloat::From4PMColors(colors, floats+0, floats+1, floats+2, floats+3);
|
2015-03-05 19:31:59 +00:00
|
|
|
|
|
|
|
SkPMColor back[4];
|
2015-03-25 20:43:34 +00:00
|
|
|
SkPMFloat::To4PMColors(floats[0], floats[1], floats[2], floats[3], back);
|
2015-03-05 19:31:59 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
REPORTER_ASSERT(r, back[i] == colors[i]);
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:43:34 +00:00
|
|
|
SkPMFloat::ClampTo4PMColors(floats[0], floats[1], floats[2], floats[3], back);
|
2015-03-05 19:31:59 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
REPORTER_ASSERT(r, back[i] == colors[i]);
|
|
|
|
}
|
2015-02-23 18:04:34 +00:00
|
|
|
}
|