1 /*
2 * Copyright 2017 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 "src/core/SkColorData.h"
11 #include "src/core/SkColorPriv.h"
12 #include "tests/Test.h"
13
14 #include <cstdint>
15
16 #define ASSERT(expr) REPORTER_ASSERT(r, expr)
17
DEF_TEST(Splay,r)18 DEF_TEST(Splay, r) {
19 const SkPMColor color = 0xA1B2C3D4;
20
21 uint32_t ag, rb;
22 SkSplay(color, &ag, &rb);
23 ASSERT(ag == 0x00A100C3);
24 ASSERT(rb == 0x00B200D4);
25 ASSERT(SkUnsplay(ag << 8, rb << 8) == color);
26
27 const uint64_t agrb = SkSplay(color);
28 ASSERT(agrb == 0x00A100C300B200D4ULL);
29 ASSERT(SkUnsplay(agrb<<8) == color);
30 }
31
DEF_TEST(FourByteInterp,r)32 DEF_TEST(FourByteInterp, r) {
33 const SkPMColor src = 0xAB998877, dst = 0x66334455;
34 for (unsigned scale = 0; scale <= 256; scale++) {
35 ASSERT(SkFourByteInterp256(src, dst, scale) == SkFastFourByteInterp256(src, dst, scale));
36 }
37
38 for (unsigned scale = 0; scale < 256; scale++) {
39 // SkFourByteInterp and SkFastFourByteInterp convert from [0, 255] to [0, 256] differently.
40 // In particular, slow may end up a little too high (weirdly, fast is more accurate).
41 const SkPMColor slow = SkFourByteInterp(src, dst, scale);
42 const SkPMColor fast = SkFastFourByteInterp(src, dst, scale);
43
44 const int deltaA = SkGetPackedA32(slow) - SkGetPackedA32(fast);
45 const int deltaR = SkGetPackedR32(slow) - SkGetPackedR32(fast);
46 const int deltaG = SkGetPackedG32(slow) - SkGetPackedG32(fast);
47 const int deltaB = SkGetPackedB32(slow) - SkGetPackedB32(fast);
48
49 ASSERT(deltaA == 0 || deltaA == 1);
50 ASSERT(deltaR == 0 || deltaR == 1);
51 ASSERT(deltaG == 0 || deltaG == 1);
52 ASSERT(deltaB == 0 || deltaB == 1);
53 }
54 }
55
DEF_TEST(SkBlendARGB32_SameOpaqueSrcAndDstWithAnyAlpha_ProducesSameColor,r)56 DEF_TEST(SkBlendARGB32_SameOpaqueSrcAndDstWithAnyAlpha_ProducesSameColor, r) {
57 SkColor colors[] = {
58 SK_ColorWHITE, SK_ColorBLACK, SK_ColorGRAY,
59 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
60 SK_ColorYELLOW, SK_ColorCYAN, SK_ColorMAGENTA,
61 SkColorSetARGB(255, 255 / 4, 255 / 3, 255 / 2),
62 // https://bugzilla.mozilla.org/show_bug.cgi?id=1200684 and https://codereview.chromium.org/2097883002
63 0xFF2E3338,
64 };
65
66 for (SkColor c: colors) {
67 SkASSERTF(SkColorGetA(c) == 0xFF, "this is only true for opaque colors");
68
69 const SkPMColor srcAndDst = SkPreMultiplyColor(c);
70
71 for (unsigned scale = 0; scale < 256; scale++) {
72 U8CPU alpha = static_cast<U8CPU>(scale);
73
74 SkPMColor output = SkBlendARGB32(srcAndDst, srcAndDst, alpha);
75 REPORTER_ASSERT(r,
76 srcAndDst == output,
77 "SkBlendARGB32(0x%08x, 0x%08x, %02x) = %08x instead of the original",
78 srcAndDst, srcAndDst, alpha, output);
79 }
80 }
81 }
82