Flutter Engine
The Flutter Engine
Functions
hsl.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkPaint.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include "include/effects/SkGradientShader.h"

Go to the source code of this file.

Functions

static float min (float r, float g, float b)
 
static float max (float r, float g, float b)
 
static float sat (float r, float g, float b)
 
static float lum (float r, float g, float b)
 
static void set_sat (float *r, float *g, float *b, float s)
 
static void clip_color (float *r, float *g, float *b)
 
static void set_lum (float *r, float *g, float *b, float l)
 
static void hue (float dr, float dg, float db, float *sr, float *sg, float *sb)
 
static void saturation (float dr, float dg, float db, float *sr, float *sg, float *sb)
 
static void color (float dr, float dg, float db, float *sr, float *sg, float *sb)
 
static void luminosity (float dr, float dg, float db, float *sr, float *sg, float *sb)
 
static SkColor blend (SkColor dst, SkColor src, void(*mode)(float, float, float, float *, float *, float *))
 
 DEF_SIMPLE_GM (hsl, canvas, 600, 100)
 
static sk_sp< SkShadermake_grad (SkScalar width)
 
 DEF_SIMPLE_GM (HSL_duck, canvas, 1110, 620)
 

Function Documentation

◆ blend()

static SkColor blend ( SkColor  dst,
SkColor  src,
void(*)(float, float, float, float *, float *, float *)  mode 
)
static

Definition at line 142 of file hsl.cpp.

143 {
144
145 SkASSERT(SkColorGetA(dst) == 0xff
146 && SkColorGetA(src) == 0xff); // Not fundamental, just simplifying for this GM.
147
148 SkColor4f d = SkColor4f::FromColor(dst),
149 s = SkColor4f::FromColor(src);
150
151 mode( d.fR, d.fG, d.fB,
152 &s.fR, &s.fG, &s.fB);
153
154 return s.toSkColor();
155}
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkColorGetA(color)
Definition: SkColor.h:61
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
struct MyStruct s
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
dst
Definition: cp.py:12

◆ clip_color()

static void clip_color ( float *  r,
float *  g,
float *  b 
)
static

Definition at line 68 of file hsl.cpp.

68 {
69 float l = lum(*r,*g,*b),
70 mn = min(*r,*g,*b),
71 mx = max(*r,*g,*b);
72 auto clip = [=](float c) {
73 if (mn < 0) { c = l + (c - l) * ( l) / (l - mn); }
74 if (mx > 1) { c = l + (c - l) * (1 - l) / (mx - l); }
75 SkASSERT(-0.0001f < c); // This may end up very slightly negative...
76 SkASSERT( c <= 1);
77 return c;
78 };
79 *r = clip(*r);
80 *g = clip(*g);
81 *b = clip(*b);
82}
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
static bool b
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float lum(float r, float g, float b)
Definition: hsl.cpp:52
static float min(float r, float g, float b)
Definition: hsl.cpp:48

◆ color()

static void color ( float  dr,
float  dg,
float  db,
float *  sr,
float *  sg,
float *  sb 
)
static

Definition at line 118 of file hsl.cpp.

119 {
120 // Hue and Saturation of Src, Luminosity of Dst.
121 float R = *sr,
122 G = *sg,
123 B = *sb;
124 set_lum(&R,&G,&B, lum(dr,dg,db));
125 *sr = R;
126 *sg = G;
127 *sb = B;
128}
static void set_lum(float *r, float *g, float *b, float l)
Definition: hsl.cpp:83
#define R(r)
#define B
Definition: SkMD5.cpp:125

◆ DEF_SIMPLE_GM() [1/2]

DEF_SIMPLE_GM ( hsl  ,
canvas  ,
600  ,
100   
)

Definition at line 157 of file hsl.cpp.

157 {
160
161 const char* comment = "HSL blend modes are correct when you see no circles in the squares.";
162 canvas->drawString(comment, 10,10, font, paint);
163
164 // Just to keep things reaaaal simple, we'll only use opaque colors.
165 SkPaint bg, fg;
166 bg.setColor(0xff00ff00); // Fully-saturated bright green, H = 120°, S = 100%, L = 50%.
167 fg.setColor(0xff7f3f7f); // Partly-saturated dim magenta, H = 300°, S = ~33%, L = ~37%.
168
169 struct {
171 void (*reference)(float,float,float, float*,float*,float*);
172 } tests[] = {
173 { SkBlendMode::kSrc, nullptr },
174 { SkBlendMode::kDst, nullptr },
179 };
180 for (auto test : tests) {
181 canvas->drawRect({20,20,80,80}, bg);
182
183 fg.setBlendMode(test.mode);
184 canvas->drawRect({20,20,80,80}, fg);
185
186 if (test.reference) {
187 SkPaint ref;
188 ref.setColor(blend(bg.getColor(), fg.getColor(), test.reference));
189 canvas->drawCircle(50,50, 20, ref);
190 }
191
192 canvas->drawString(SkBlendMode_Name(test.mode), 20, 90, font, paint);
193
194 canvas->translate(100,0);
195 }
196}
static BlurTest tests[]
Definition: BlurTest.cpp:84
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition: SkBlendMode.h:38
@ kSaturation
saturation of source with hue and luminosity of destination
@ kHue
hue of source with saturation and luminosity of destination
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kColor
hue and saturation of source with luminosity of destination
Definition: SkFont.h:35
void setColor(SkColor color)
Definition: SkPaint.cpp:119
SkColor getColor() const
Definition: SkPaint.h:225
void setBlendMode(SkBlendMode mode)
Definition: SkPaint.cpp:151
const Paint & paint
Definition: color_source.cc:38
static SkColor blend(SkColor dst, SkColor src, void(*mode)(float, float, float, float *, float *, float *))
Definition: hsl.cpp:142
static void color(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:118
static void luminosity(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:130
static void hue(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:92
static void saturation(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:105
SkFont DefaultPortableFont()
font
Font Metadata and Metrics.

◆ DEF_SIMPLE_GM() [2/2]

DEF_SIMPLE_GM ( HSL_duck  ,
canvas  ,
1110  ,
620   
)

Definition at line 212 of file hsl.cpp.

212 {
213 auto src = ToolUtils::GetResourceAsImage("images/ducky.png");
214 auto dst = make_grad(src->width());
215 SkRect r = SkRect::MakeIWH(src->width(), src->height());
216
217 canvas->translate(10, 50);
218 canvas->scale(0.5f, 0.5f);
219
220 const struct {
222 const char* fName;
223 } recs[] = {
224 { SkBlendMode::kHue, "Hue" },
225 { SkBlendMode::kSaturation, "Saturation" },
226 { SkBlendMode::kColor, "Color" },
227 { SkBlendMode::kLuminosity, "Luminosity" },
228 };
229
231 font.setSize(40);
233
234 canvas->save();
235 for (auto [_, name] : recs) {
236 canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, 150, -20,
237 font, SkPaint());
238 canvas->translate(r.width() + 10, 0);
239 }
240 canvas->restore();
241
242 for (SkScalar src_a : {1.0f, 0.5f}) {
243 canvas->save();
244 for (auto [mode, _] : recs) {
245 SkPaint p;
246 p.setShader(dst);
247 canvas->drawRect(r, p); // bg
248
249 p.setShader(nullptr);
250 p.setBlendMode(mode);
251 p.setAlphaf(src_a);
252 canvas->drawImageRect(src, r, SkSamplingOptions(), &p);
253
254 canvas->translate(r.width() + 10, 0);
255 }
256 SkString str;
257 str.printf("alpha %g", src_a);
258 canvas->drawSimpleText(str.c_str(), str.size(), SkTextEncoding::kUTF8, 10, r.height()/2,
259 font, SkPaint());
260
261 canvas->restore();
262 canvas->translate(0, r.height() + 10);
263 }
264}
const char * fName
@ kUTF8
uses bytes to represent UTF-8 or ASCII
@ kAntiAlias
may have transparent pixels on glyph edges
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
size_t size() const
Definition: SkString.h:131
const char * c_str() const
Definition: SkString.h:133
float SkScalar
Definition: extension.cpp:12
static sk_sp< SkShader > make_grad(SkScalar width)
Definition: hsl.cpp:202
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
SkSamplingOptions(SkFilterMode::kLinear))
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
SkBlendMode fMode
Definition: xfermodes.cpp:52

◆ hue()

static void hue ( float  dr,
float  dg,
float  db,
float *  sr,
float *  sg,
float *  sb 
)
static

Definition at line 92 of file hsl.cpp.

93 {
94 // Hue of Src, Saturation and Luminosity of Dst.
95 float R = *sr,
96 G = *sg,
97 B = *sb;
98 set_sat(&R,&G,&B, sat(dr,dg,db));
99 set_lum(&R,&G,&B, lum(dr,dg,db));
100 *sr = R;
101 *sg = G;
102 *sb = B;
103}
static void set_sat(float *r, float *g, float *b, float s)
Definition: hsl.cpp:57
static float sat(float r, float g, float b)
Definition: hsl.cpp:51

◆ lum()

static float lum ( float  r,
float  g,
float  b 
)
static

Definition at line 52 of file hsl.cpp.

52{ return r*0.30f + g*0.59f + b*0.11f; }

◆ luminosity()

static void luminosity ( float  dr,
float  dg,
float  db,
float *  sr,
float *  sg,
float *  sb 
)
static

Definition at line 130 of file hsl.cpp.

131 {
132 // Luminosity of Src, Hue and Saturation of Dst.
133 float R = dr,
134 G = dg,
135 B = db;
136 set_lum(&R,&G,&B, lum(*sr,*sg,*sb));
137 *sr = R;
138 *sg = G;
139 *sb = B;
140}

◆ make_grad()

static sk_sp< SkShader > make_grad ( SkScalar  width)
static

Definition at line 202 of file hsl.cpp.

202 {
203 SkColor colors[] = {
204 0xFF00CCCC, 0xFF0000CC, 0xFFCC00CC, 0xFFCC0000, 0xFFCCCC00, 0xFF00CC00,
205 };
206 SkPoint pts[] = {{0, 0}, {width, 0}};
207
210}
uint32_t SkColor
Definition: SkColor.h:37
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
PODArray< SkColor > colors
Definition: SkRecords.h:276
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
int32_t width

◆ max()

static float max ( float  r,
float  g,
float  b 
)
static

Definition at line 49 of file hsl.cpp.

49{ return std::max(r, std::max(g, b)); }

◆ min()

static float min ( float  r,
float  g,
float  b 
)
static

Definition at line 48 of file hsl.cpp.

48{ return std::min(r, std::min(g, b)); }

◆ sat()

static float sat ( float  r,
float  g,
float  b 
)
static

Definition at line 51 of file hsl.cpp.

51{ return max(r,g,b) - min(r,g,b); }

◆ saturation()

static void saturation ( float  dr,
float  dg,
float  db,
float *  sr,
float *  sg,
float *  sb 
)
static

Definition at line 105 of file hsl.cpp.

106 {
107 // Saturation of Src, Hue and Luminosity of Dst
108 float R = dr,
109 G = dg,
110 B = db;
111 set_sat(&R,&G,&B, sat(*sr,*sg,*sb));
112 set_lum(&R,&G,&B, lum( dr, dg, db)); // This may seem redundant, but it is not.
113 *sr = R;
114 *sg = G;
115 *sb = B;
116}

◆ set_lum()

static void set_lum ( float *  r,
float *  g,
float *  b,
float  l 
)
static

Definition at line 83 of file hsl.cpp.

83 {
84 float diff = l - lum(*r,*g,*b);
85 *r += diff;
86 *g += diff;
87 *b += diff;
88 clip_color(r,g,b);
89}
static void clip_color(float *r, float *g, float *b)
Definition: hsl.cpp:68

◆ set_sat()

static void set_sat ( float *  r,
float *  g,
float *  b,
float  s 
)
static

Definition at line 57 of file hsl.cpp.

57 {
58 float mn = min(*r,*g,*b),
59 mx = max(*r,*g,*b);
60 auto channel = [=](float c) {
61 return mx == mn ? 0
62 : (c - mn) * s / (mx - mn);
63 };
64 *r = channel(*r);
65 *g = channel(*g);
66 *b = channel(*b);
67}