2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkColorPriv.h"
11 #include "SkColorSpace_Base.h"
13 #include "SkSurface.h"
15 #include "SkColorMatrixFilter.h"
16 #include "SkGradientShader.h"
18 static sk_sp<SkShader> make_opaque_color() {
19 return SkShader::MakeColorShader(0xFFFF0000);
22 static sk_sp<SkShader> make_alpha_color() {
23 return SkShader::MakeColorShader(0x80FF0000);
26 static sk_sp<SkColorFilter> make_cf_null() {
30 static sk_sp<SkColorFilter> make_cf0() {
32 cm.setSaturation(0.75f);
33 return SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat);
36 static sk_sp<SkColorFilter> make_cf1() {
38 cm.setSaturation(0.75f);
39 auto a(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
40 // CreateComposedFilter will try to concat these two matrices, resulting in a single
41 // filter (which is good for speed). For this test, we want to force a real compose of
42 // these two, so our inner filter has a scale-up, which disables the optimization of
43 // combining the two matrices.
44 cm.setScale(1.1f, 0.9f, 1);
45 auto b(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
46 return SkColorFilter::MakeComposeFilter(a, b);
49 static sk_sp<SkColorFilter> make_cf2() {
50 return SkColorFilter::MakeModeFilter(0x8044CC88, SkBlendMode::kSrcATop);
53 static void draw_into_canvas(SkCanvas* canvas) {
54 const SkRect r = SkRect::MakeWH(50, 100);
55 sk_sp<SkShader> (*shaders[])() { make_opaque_color, make_alpha_color };
56 sk_sp<SkColorFilter> (*filters[])() { make_cf_null, make_cf0, make_cf1, make_cf2 };
59 for (auto shProc : shaders) {
60 paint.setShader(shProc());
61 for (auto cfProc : filters) {
62 paint.setColorFilter(cfProc());
63 canvas->drawRect(r, paint);
64 canvas->translate(60, 0);
69 DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
70 canvas->translate(10, 10);
73 // need the target to be opaque, so we can draw it to the screen
74 // even if it holds sRGB values.
75 bg.setColor(0xFFFFFFFF);
77 sk_sp<SkColorSpace> colorSpaces[]{
79 SkColorSpace::MakeSRGB()
81 for (auto colorSpace : colorSpaces) {
82 const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
84 auto surface(SkSurface::MakeRaster(info));
85 surface->getCanvas()->drawPaint(bg);
86 draw_into_canvas(surface->getCanvas());
87 surface->draw(canvas, 0, 0, nullptr);
88 canvas->translate(0, 120);
92 ///////////////////////////////////////////////////////////////////////////////////////////////////
93 #include "SkColorSpace.h"
95 DEF_SIMPLE_GM(color4shader, canvas, 360, 480) {
96 canvas->translate(10, 10);
98 auto srgb = SkColorSpace::MakeSRGB();
100 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
101 // red -> blue, green -> red, blue -> green (sRGB)
102 mat.set3x3(0, 0, 1, 1, 0, 0, 0, 1, 0);
103 mat.postConcat(*as_CSB(srgb)->toXYZD50());
105 const SkColor4f colors[] {
109 { 0.5, 0.5, 0.5, 1 },
113 SkRect r = SkRect::MakeWH(100, 100);
115 for (const auto& c4 : colors) {
116 sk_sp<SkShader> shaders[] {
117 SkShader::MakeColorShader(c4, nullptr),
118 SkShader::MakeColorShader(c4, srgb),
119 SkShader::MakeColorShader(c4,
120 SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma, mat)),
124 for (const auto& s : shaders) {
126 canvas->drawRect(r, paint);
127 canvas->translate(r.width() * 6 / 5, 0);
130 canvas->translate(0, r.height() * 6 / 5);