Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
color4f.cpp
Go to the documentation of this file.
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 "gm/gm.h"
16#include "include/core/SkRect.h"
21
23 return SkShaders::Color(0xFFFF0000);
24}
25
27 return SkShaders::Color(0x80FF0000);
28}
29
31 return nullptr;
32}
33
36 cm.setSaturation(0.75f);
37 return SkColorFilters::Matrix(cm);
38}
39
42 cm.setSaturation(0.75f);
43 auto a = SkColorFilters::Matrix(cm);
44 // CreateComposedFilter will try to concat these two matrices, resulting in a single
45 // filter (which is good for speed). For this test, we want to force a real compose of
46 // these two, so our inner filter has a scale-up, which disables the optimization of
47 // combining the two matrices.
48 cm.setScale(1.1f, 0.9f, 1);
49 return a->makeComposed(SkColorFilters::Matrix(cm));
50}
51
55
56static void draw_into_canvas(SkCanvas* canvas) {
57 const SkRect r = SkRect::MakeWH(50, 100);
60
62 for (auto shProc : shaders) {
63 paint.setShader(shProc());
64 for (auto cfProc : filters) {
65 paint.setColorFilter(cfProc());
66 canvas->drawRect(r, paint);
67 canvas->translate(60, 0);
68 }
69 }
70}
71
72DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
73 canvas->translate(10, 10);
74
75 SkPaint bg;
76 // need the target to be opaque, so we can draw it to the screen
77 // even if it holds sRGB values.
78 bg.setColor(0xFFFFFFFF);
79
80 sk_sp<SkColorSpace> colorSpaces[]{
81 nullptr,
83 };
84 for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) {
85 const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
86 colorSpace);
88 surface->getCanvas()->drawPaint(bg);
89 draw_into_canvas(surface->getCanvas());
90 surface->draw(canvas, 0, 0);
91 canvas->translate(0, 120);
92 }
93}
94
95///////////////////////////////////////////////////////////////////////////////////////////////////
96
97DEF_SIMPLE_GM(color4shader, canvas, 360, 480) {
98 canvas->translate(10, 10);
99
100 auto srgb = SkColorSpace::MakeSRGB();
101 auto spin = srgb->makeColorSpin(); // RGB -> GBR
102
103 const SkColor4f colors[] {
104 { 1, 0, 0, 1 },
105 { 0, 1, 0, 1 },
106 { 0, 0, 1, 1 },
107 { 0.5, 0.5, 0.5, 1 },
108 };
109
111 SkRect r = SkRect::MakeWH(100, 100);
112
113 for (const auto& c4 : colors) {
114 sk_sp<SkShader> shaders[] {
115 SkShaders::Color(c4, nullptr),
116 SkShaders::Color(c4, srgb),
117 SkShaders::Color(c4, spin),
118 };
119
120 canvas->save();
121 for (const auto& s : shaders) {
122 paint.setShader(s);
123 canvas->drawRect(r, paint);
124 canvas->translate(r.width() * 6 / 5, 0);
125 }
126 canvas->restore();
127 canvas->translate(0, r.height() * 6 / 5);
128 }
129}
130
131DEF_SIMPLE_GM(color4blendcf, canvas, 360, 480) {
132 canvas->translate(10, 10);
133
134 auto srgb = SkColorSpace::MakeSRGB();
135 auto spin = srgb->makeColorSpin(); // RGB -> GBR
136
137 const SkColor4f colors[] {
138 { 1, 0, 0, 1 },
139 { 0, 1, 0, 1 },
140 { 0, 0, 1, 1 },
141 { 0.5, 0.5, 0.5, 1 },
142 };
143
145 paint.setColor(SK_ColorWHITE);
146 SkRect r = SkRect::MakeWH(100, 100);
147
148 for (const auto& c4 : colors) {
149 sk_sp<SkColorFilter> filters[] {
150 // Use kModulate and a paint color of white so the final drawn color is color-space
151 // managed 'c4'.
155 };
156
157 canvas->save();
158 for (const auto& f : filters) {
159 paint.setColorFilter(f);
160 canvas->drawRect(r, paint);
161 canvas->translate(r.width() * 6 / 5, 0);
162 }
163 canvas->restore();
164 canvas->translate(0, r.height() * 6 / 5);
165 }
166}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kModulate
r = s*d
@ kSrcATop
r = s*da + d*(1-sa)
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void setSaturation(float sat)
void setScale(float rScale, float gScale, float bScale, float aScale=1.0f)
static sk_sp< SkColorSpace > MakeSRGB()
void setColor(SkColor color)
Definition SkPaint.cpp:119
static sk_sp< SkShader > make_opaque_color()
Definition color4f.cpp:22
static sk_sp< SkShader > make_alpha_color()
Definition color4f.cpp:26
static sk_sp< SkColorFilter > make_cf_null()
Definition color4f.cpp:30
static sk_sp< SkColorFilter > make_cf0()
Definition color4f.cpp:34
static sk_sp< SkColorFilter > make_cf1()
Definition color4f.cpp:40
static void draw_into_canvas(SkCanvas *canvas)
Definition color4f.cpp:56
static sk_sp< SkColorFilter > make_cf2()
Definition color4f.cpp:52
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
struct MyStruct s
struct MyStruct a[10]
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static sk_sp< SkColorFilter > spin(sk_sp< SkColorFilter > cf)