OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 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 "SkBitmap.h" | |
9 #include "SkCanvas.h" | |
10 #include "SkColor.h" | |
11 #include "SkColorMatrixFilter.h" | |
12 #include "SkPaint.h" | |
13 | |
14 #include "Test.h" | |
15 | |
16 static inline void assert_color(skiatest::Reporter* reporter, SkColor expected, SkColor actual) { | |
17 const int TOLERANCE = 1; | |
18 REPORTER_ASSERT(reporter, abs(SkColorGetA(expected) - SkColorGetA(actual)) < = TOLERANCE); | |
19 REPORTER_ASSERT(reporter, abs(SkColorGetR(expected) - SkColorGetR(actual)) < = TOLERANCE); | |
20 REPORTER_ASSERT(reporter, abs(SkColorGetG(expected) - SkColorGetG(actual)) < = TOLERANCE); | |
21 REPORTER_ASSERT(reporter, abs(SkColorGetB(expected) - SkColorGetB(actual)) < = TOLERANCE); | |
22 } | |
23 | |
24 /** | |
25 * This test case is a mirror of the Android CTS tests for MatrixColorFilter | |
26 * found in the android.graphics.ColorMatrixColorFilterTest class. | |
27 */ | |
28 static inline void test_colorMatrix(skiatest::Reporter* reporter) { | |
scroggo
2015/05/19 20:10:44
Why is this its own function? Why not just use DEF
djsollen
2015/05/19 20:24:29
Personal preference. My intention was that this m
| |
29 | |
30 SkBitmap bitmap; | |
31 bitmap.allocN32Pixels(1,1); | |
32 | |
33 SkCanvas canvas(bitmap); | |
34 SkPaint paint; | |
35 | |
36 SkScalar blueToCyan[20] = { | |
37 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, | |
38 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, | |
39 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, | |
40 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; | |
41 paint.setColorFilter(SkColorMatrixFilter::Create(blueToCyan))->unref(); | |
42 | |
43 paint.setColor(SK_ColorBLUE); | |
44 canvas.drawPoint(0, 0, paint); | |
45 assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0)); | |
46 | |
47 paint.setColor(SK_ColorGREEN); | |
48 canvas.drawPoint(0, 0, paint); | |
49 assert_color(reporter, SK_ColorGREEN, bitmap.getColor(0, 0)); | |
50 | |
51 paint.setColor(SK_ColorRED); | |
52 canvas.drawPoint(0, 0, paint); | |
53 assert_color(reporter, SK_ColorRED, bitmap.getColor(0, 0)); | |
54 | |
55 // color components are clipped, not scaled | |
56 paint.setColor(SK_ColorMAGENTA); | |
57 canvas.drawPoint(0, 0, paint); | |
58 assert_color(reporter, SK_ColorWHITE, bitmap.getColor(0, 0)); | |
59 | |
60 SkScalar transparentRedAddBlue[20] = { | |
61 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, | |
62 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, | |
63 0.0f, 0.0f, 1.0f, 0.0f, 64.0f, | |
64 -0.5f, 0.0f, 0.0f, 1.0f, 0.0f | |
65 }; | |
66 paint.setColorFilter(SkColorMatrixFilter::Create(transparentRedAddBlue))->un ref(); | |
67 bitmap.eraseColor(SK_ColorTRANSPARENT); | |
68 | |
69 paint.setColor(SK_ColorRED); | |
70 canvas.drawPoint(0, 0, paint); | |
71 // Temporarily disabled assert until skbug.com/3848 is resolved. | |
72 // assert_color(reporter, SkColorSetARGB(128, 255, 0, 64), bitmap.getColor(0 , 0)); | |
73 | |
74 paint.setColor(SK_ColorCYAN); | |
75 canvas.drawPoint(0, 0, paint); | |
76 // blue gets clipped | |
77 assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0)); | |
78 | |
79 // change array to filter out green | |
80 REPORTER_ASSERT(reporter, 1.0f == transparentRedAddBlue[6]); | |
81 transparentRedAddBlue[6] = 0.0f; | |
82 | |
83 // check that changing the array has no effect | |
84 canvas.drawPoint(0, 0, paint); | |
85 assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0)); | |
86 | |
87 // create a new filter with the changed matrix | |
88 paint.setColorFilter(SkColorMatrixFilter::Create(transparentRedAddBlue))->un ref(); | |
89 canvas.drawPoint(0, 0, paint); | |
90 assert_color(reporter, SK_ColorBLUE, bitmap.getColor(0, 0)); | |
91 } | |
92 | |
93 DEF_TEST(ColorMatrix, reporter) { | |
94 test_colorMatrix(reporter); | |
95 } | |
OLD | NEW |