2016-01-22 09:04:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-04-08 21:24:37 +00:00
|
|
|
#include "SkBitmapProcShader.h"
|
2016-01-22 09:04:29 +00:00
|
|
|
#include "SkColor.h"
|
2016-01-23 22:19:06 +00:00
|
|
|
#include "SkColorMatrixFilter.h"
|
2016-01-24 03:37:56 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkImage.h"
|
2016-02-18 20:39:14 +00:00
|
|
|
#include "SkPM4f.h"
|
2016-01-24 03:37:56 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
2016-01-22 09:04:29 +00:00
|
|
|
#include "Test.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
2016-01-24 03:37:56 +00:00
|
|
|
const float kTolerance = 1.0f / (1 << 20);
|
|
|
|
|
|
|
|
static bool nearly_equal(float a, float b, float tol = kTolerance) {
|
|
|
|
SkASSERT(tol >= 0);
|
|
|
|
return fabsf(a - b) <= tol;
|
|
|
|
}
|
|
|
|
|
2016-01-22 09:04:29 +00:00
|
|
|
DEF_TEST(SkColor4f_FromColor, reporter) {
|
|
|
|
const struct {
|
|
|
|
SkColor fC;
|
|
|
|
SkColor4f fC4;
|
|
|
|
} recs[] = {
|
2016-06-24 13:31:47 +00:00
|
|
|
{ SK_ColorBLACK, { 0, 0, 0, 1 } },
|
2016-01-22 09:04:29 +00:00
|
|
|
{ SK_ColorWHITE, { 1, 1, 1, 1 } },
|
2016-06-24 13:31:47 +00:00
|
|
|
{ SK_ColorRED, { 1, 0, 0, 1 } },
|
|
|
|
{ SK_ColorGREEN, { 0, 1, 0, 1 } },
|
|
|
|
{ SK_ColorBLUE, { 0, 0, 1, 1 } },
|
2016-01-22 09:04:29 +00:00
|
|
|
{ 0, { 0, 0, 0, 0 } },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& r : recs) {
|
|
|
|
SkColor4f c4 = SkColor4f::FromColor(r.fC);
|
|
|
|
REPORTER_ASSERT(reporter, c4 == r.fC4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:37:56 +00:00
|
|
|
DEF_TEST(Color4f_premul, reporter) {
|
2016-01-22 09:04:29 +00:00
|
|
|
SkRandom rand;
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000000; ++i) {
|
|
|
|
// First just test opaque colors, so that the premul should be exact
|
|
|
|
SkColor4f c4 {
|
2016-06-24 13:31:47 +00:00
|
|
|
rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1(), 1
|
2016-01-22 09:04:29 +00:00
|
|
|
};
|
|
|
|
SkPM4f pm4 = c4.premul();
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, pm4.a() == c4.fA);
|
|
|
|
REPORTER_ASSERT(reporter, pm4.r() == c4.fA * c4.fR);
|
|
|
|
REPORTER_ASSERT(reporter, pm4.g() == c4.fA * c4.fG);
|
|
|
|
REPORTER_ASSERT(reporter, pm4.b() == c4.fA * c4.fB);
|
2016-01-22 09:04:29 +00:00
|
|
|
|
|
|
|
// We compare with a tolerance, in case our premul multiply is implemented at slightly
|
|
|
|
// different precision than the test code.
|
|
|
|
c4.fA = rand.nextUScalar1();
|
|
|
|
pm4 = c4.premul();
|
|
|
|
REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA);
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(pm4.r(), c4.fA * c4.fR));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(pm4.g(), c4.fA * c4.fG));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(pm4.b(), c4.fA * c4.fB));
|
2016-01-22 09:04:29 +00:00
|
|
|
}
|
|
|
|
}
|