Flutter Engine
The Flutter Engine
Functions
ColorMatrixTest.cpp File Reference
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRefCnt.h"
#include "tests/Test.h"
#include <cmath>

Go to the source code of this file.

Functions

static void assert_color (skiatest::Reporter *reporter, SkColor expected, SkColor actual, int tolerance)
 
static void assert_color (skiatest::Reporter *reporter, SkColor expected, SkColor actual)
 
static void test_colorMatrixCTS (skiatest::Reporter *reporter)
 
 DEF_TEST (ColorMatrix, reporter)
 
 DEF_TEST (ColorMatrix_clamp_while_unpremul, r)
 

Function Documentation

◆ assert_color() [1/2]

static void assert_color ( skiatest::Reporter reporter,
SkColor  expected,
SkColor  actual 
)
inlinestatic

Definition at line 26 of file ColorMatrixTest.cpp.

26 {
27 const int TOLERANCE = 1;
28 assert_color(reporter, expected, actual, TOLERANCE);
29}
static void assert_color(skiatest::Reporter *reporter, SkColor expected, SkColor actual, int tolerance)
reporter
Definition: FontMgrTest.cpp:39

◆ assert_color() [2/2]

static void assert_color ( skiatest::Reporter reporter,
SkColor  expected,
SkColor  actual,
int  tolerance 
)
inlinestatic

Definition at line 18 of file ColorMatrixTest.cpp.

19 {
20 REPORTER_ASSERT(reporter, abs((int)(SkColorGetA(expected) - SkColorGetA(actual))) <= tolerance);
21 REPORTER_ASSERT(reporter, abs((int)(SkColorGetR(expected) - SkColorGetR(actual))) <= tolerance);
22 REPORTER_ASSERT(reporter, abs((int)(SkColorGetG(expected) - SkColorGetG(actual))) <= tolerance);
23 REPORTER_ASSERT(reporter, abs((int)(SkColorGetB(expected) - SkColorGetB(actual))) <= tolerance);
24}
#define SkColorGetR(color)
Definition: SkColor.h:65
#define SkColorGetG(color)
Definition: SkColor.h:69
#define SkColorGetA(color)
Definition: SkColor.h:61
#define SkColorGetB(color)
Definition: SkColor.h:73
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707

◆ DEF_TEST() [1/2]

DEF_TEST ( ColorMatrix  ,
reporter   
)

Definition at line 99 of file ColorMatrixTest.cpp.

99 {
101}
static void test_colorMatrixCTS(skiatest::Reporter *reporter)

◆ DEF_TEST() [2/2]

DEF_TEST ( ColorMatrix_clamp_while_unpremul  ,
 
)

Definition at line 104 of file ColorMatrixTest.cpp.

104 {
105 // This matrix does green += 255/255 and alpha += 32/255. We want to test
106 // that if we pass it opaque alpha and small red and blue values, red and
107 // blue stay unchanged, not pumped up by that ~1.12 intermediate alpha.
108 float m[] = {
109 1, 0, 0, 0, 0,
110 0, 1, 0, 0, 1,
111 0, 0, 1, 0, 0,
112 0, 0, 0, 1, 32.0f/255,
113 };
114 auto filter = SkColorFilters::Matrix(m);
115
116 SkColor4f srcColor = SkColor4f::FromColor(0xff0a0b0c);
117 SkColor filtered = filter->filterColor4f(srcColor, nullptr, nullptr).toSkColor();
122}
uint32_t SkColor
Definition: SkColor.h:37
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
def filtered(names, to_skip)
Definition: zip_utils.py:20

◆ test_colorMatrixCTS()

static void test_colorMatrixCTS ( skiatest::Reporter reporter)
inlinestatic

This test case is a mirror of the Android CTS tests for MatrixColorFilter found in the android.graphics.ColorMatrixColorFilterTest class.

Definition at line 35 of file ColorMatrixTest.cpp.

35 {
36
38 bitmap.allocN32Pixels(1,1);
39
40 SkCanvas canvas(bitmap);
42
43 float blueToCyan[20] = {
44 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
45 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
46 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
47 0.0f, 0.0f, 0.0f, 1.0f, 0.0f };
48 paint.setColorFilter(SkColorFilters::Matrix(blueToCyan));
49
50 paint.setColor(SK_ColorBLUE);
51 canvas.drawPoint(0, 0, paint);
52 assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
53
54 paint.setColor(SK_ColorGREEN);
55 canvas.drawPoint(0, 0, paint);
56 assert_color(reporter, SK_ColorGREEN, bitmap.getColor(0, 0));
57
58 paint.setColor(SK_ColorRED);
59 canvas.drawPoint(0, 0, paint);
60 assert_color(reporter, SK_ColorRED, bitmap.getColor(0, 0));
61
62 // color components are clipped, not scaled
63 paint.setColor(SK_ColorMAGENTA);
64 canvas.drawPoint(0, 0, paint);
65 assert_color(reporter, SK_ColorWHITE, bitmap.getColor(0, 0));
66
67 float transparentRedAddBlue[20] = {
68 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
69 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
70 0.0f, 0.0f, 1.0f, 0.0f, 64.0f/255,
71 -0.5f, 0.0f, 0.0f, 1.0f, 0.0f
72 };
73 paint.setColorFilter(SkColorFilters::Matrix(transparentRedAddBlue));
74 bitmap.eraseColor(SK_ColorTRANSPARENT);
75
76 paint.setColor(SK_ColorRED);
77 canvas.drawPoint(0, 0, paint);
78 assert_color(reporter, SkColorSetARGB(128, 255, 0, 64), bitmap.getColor(0, 0), 2);
79
80 paint.setColor(SK_ColorCYAN);
81 canvas.drawPoint(0, 0, paint);
82 // blue gets clipped
83 assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
84
85 // change array to filter out green
86 REPORTER_ASSERT(reporter, 1.0f == transparentRedAddBlue[6]);
87 transparentRedAddBlue[6] = 0.0f;
88
89 // check that changing the array has no effect
90 canvas.drawPoint(0, 0, paint);
91 assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
92
93 // create a new filter with the changed matrix
94 paint.setColorFilter(SkColorFilters::Matrix(transparentRedAddBlue));
95 canvas.drawPoint(0, 0, paint);
96 assert_color(reporter, SK_ColorBLUE, bitmap.getColor(0, 0));
97}
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
const Paint & paint
Definition: color_source.cc:38
Definition: bitmap.py:1