blob: f268e5f42d357d6a536a64753ab62009c580a2a9 [file] [log] [blame]
reed6d3cef92016-01-22 01:04:29 -08001/*
2 * Copyright 2016 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
herb42da45d2016-04-08 14:24:37 -07008#include "SkBitmapProcShader.h"
reed6d3cef92016-01-22 01:04:29 -08009#include "SkColor.h"
reed5f34e8e2016-01-23 14:19:06 -080010#include "SkColorMatrixFilter.h"
reed7f225cf2016-01-23 19:37:56 -080011#include "SkGradientShader.h"
12#include "SkImage.h"
reeddd9ffea2016-02-18 12:39:14 -080013#include "SkPM4f.h"
reed7f225cf2016-01-23 19:37:56 -080014#include "SkShader.h"
15
reed6d3cef92016-01-22 01:04:29 -080016#include "Test.h"
17#include "SkRandom.h"
18
reed7f225cf2016-01-23 19:37:56 -080019const float kTolerance = 1.0f / (1 << 20);
20
21static bool nearly_equal(float a, float b, float tol = kTolerance) {
22 SkASSERT(tol >= 0);
23 return fabsf(a - b) <= tol;
24}
25
reed6d3cef92016-01-22 01:04:29 -080026DEF_TEST(SkColor4f_FromColor, reporter) {
27 const struct {
28 SkColor fC;
29 SkColor4f fC4;
30 } recs[] = {
brianosmane074d1f2016-06-24 06:31:47 -070031 { SK_ColorBLACK, { 0, 0, 0, 1 } },
reed6d3cef92016-01-22 01:04:29 -080032 { SK_ColorWHITE, { 1, 1, 1, 1 } },
brianosmane074d1f2016-06-24 06:31:47 -070033 { SK_ColorRED, { 1, 0, 0, 1 } },
34 { SK_ColorGREEN, { 0, 1, 0, 1 } },
35 { SK_ColorBLUE, { 0, 0, 1, 1 } },
reed6d3cef92016-01-22 01:04:29 -080036 { 0, { 0, 0, 0, 0 } },
reed6d3cef92016-01-22 01:04:29 -080037 };
38
39 for (const auto& r : recs) {
40 SkColor4f c4 = SkColor4f::FromColor(r.fC);
41 REPORTER_ASSERT(reporter, c4 == r.fC4);
42 }
43}
44
reed7f225cf2016-01-23 19:37:56 -080045DEF_TEST(Color4f_premul, reporter) {
reed6d3cef92016-01-22 01:04:29 -080046 SkRandom rand;
47
48 for (int i = 0; i < 1000000; ++i) {
49 // First just test opaque colors, so that the premul should be exact
50 SkColor4f c4 {
brianosmane074d1f2016-06-24 06:31:47 -070051 rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1(), 1
reed6d3cef92016-01-22 01:04:29 -080052 };
53 SkPM4f pm4 = c4.premul();
reed93bb0802016-03-08 10:09:18 -080054 REPORTER_ASSERT(reporter, pm4.a() == c4.fA);
55 REPORTER_ASSERT(reporter, pm4.r() == c4.fA * c4.fR);
56 REPORTER_ASSERT(reporter, pm4.g() == c4.fA * c4.fG);
57 REPORTER_ASSERT(reporter, pm4.b() == c4.fA * c4.fB);
reed6d3cef92016-01-22 01:04:29 -080058
59 // We compare with a tolerance, in case our premul multiply is implemented at slightly
60 // different precision than the test code.
61 c4.fA = rand.nextUScalar1();
62 pm4 = c4.premul();
63 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA);
reed93bb0802016-03-08 10:09:18 -080064 REPORTER_ASSERT(reporter, nearly_equal(pm4.r(), c4.fA * c4.fR));
65 REPORTER_ASSERT(reporter, nearly_equal(pm4.g(), c4.fA * c4.fG));
66 REPORTER_ASSERT(reporter, nearly_equal(pm4.b(), c4.fA * c4.fB));
reed6d3cef92016-01-22 01:04:29 -080067 }
68}