1 /*
2 * Copyright 2011 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
8 #include "include/core/SkColor.h"
9 #include "include/core/SkTypes.h"
10 #include "include/core/SkUnPreMultiply.h"
11 #include "include/private/base/SkCPUTypes.h"
12 #include "include/private/chromium/SkPMColor.h"
13 #include "src/base/SkMathPriv.h"
14 #include "src/base/SkRandom.h"
15 #include "src/core/SkColorData.h"
16
17 #include "tests/Test.h"
18
DEF_TEST(ColorPremul,reporter)19 DEF_TEST(ColorPremul, reporter) {
20 for (int a = 0; a <= 255; a++) {
21 for (int x = 0; x <= 255; x++) {
22 SkColor c0 = SkColorSetARGB(a, x, x, x);
23 SkPMColor p0 = SkPreMultiplyColor(c0);
24
25 SkColor c1 = SkUnPreMultiply::PMColorToColor(p0);
26 SkPMColor p1 = SkPreMultiplyColor(c1);
27
28 // we can't promise that c0 == c1, since c0 -> p0 is a many to one
29 // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1
30 REPORTER_ASSERT(reporter, p0 == p1);
31
32 {
33 int ax = SkMulDiv255Ceiling(x, a);
34 REPORTER_ASSERT(reporter, ax <= a);
35 }
36 }
37 }
38 }
39
DEF_TEST(SkPMColor_SetAndRetrieveChannels,reporter)40 DEF_TEST(SkPMColor_SetAndRetrieveChannels, reporter) {
41 SkPMColor pmc = SkPMColorSetARGB(0xFE, 0xDC, 0xBA, 0x98);
42
43 #if defined(SK_PMCOLOR_IS_RGBA)
44 REPORTER_ASSERT(reporter, pmc == 0xFE98BADC);
45 #else
46 REPORTER_ASSERT(reporter, pmc == 0xFEDCBA98);
47 #endif
48
49 REPORTER_ASSERT(reporter, SkPMColorGetA(pmc) == 0xFE);
50 REPORTER_ASSERT(reporter, SkPMColorGetR(pmc) == 0xDC);
51 REPORTER_ASSERT(reporter, SkPMColorGetG(pmc) == 0xBA);
52 REPORTER_ASSERT(reporter, SkPMColorGetB(pmc) == 0x98);
53 }
54
55 /**
56 This test fails: SkFourByteInterp does *not* preserve opaque destinations.
57 SkAlpha255To256 implemented as (alpha + 1) is faster than
58 (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out.
59 */
DEF_TEST(ColorInterp,reporter)60 DEF_TEST(ColorInterp, reporter) {
61 SkRandom r;
62
63 U8CPU a0 = 0;
64 U8CPU a255 = 255;
65 for (int i = 0; i < 200; i++) {
66 SkColor colorSrc = r.nextU();
67 SkColor colorDst = r.nextU();
68 SkPMColor src = SkPreMultiplyColor(colorSrc);
69 SkPMColor dst = SkPreMultiplyColor(colorDst);
70
71 if ((false)) {
72 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
73 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
74 }
75 }
76 }
77
DEF_TEST(ColorFastIterp,reporter)78 DEF_TEST(ColorFastIterp, reporter) {
79 SkRandom r;
80
81 U8CPU a0 = 0;
82 U8CPU a255 = 255;
83 for (int i = 0; i < 200; i++) {
84 SkColor colorSrc = r.nextU();
85 SkColor colorDst = r.nextU();
86 SkPMColor src = SkPreMultiplyColor(colorSrc);
87 SkPMColor dst = SkPreMultiplyColor(colorDst);
88
89 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst);
90 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src);
91 }
92 }
93