Flutter Engine
The Flutter Engine
50_percent_gray.cpp
Go to the documentation of this file.
1// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4REG_FIDDLE_SRGB(50_percent_gray, 530, 150, false, 0, 0, false) {
5static sk_sp<SkShader> make_bw_dither() {
7 surf->getCanvas()->drawColor(SK_ColorWHITE);
8 surf->getCanvas()->drawRect({0, 0, 1, 1}, SkPaint());
9 surf->getCanvas()->drawRect({1, 1, 2, 2}, SkPaint());
10 return surf->makeImageSnapshot()->makeShader(SkTileMode::kRepeat,
13}
14
15void draw(SkCanvas* canvas) {
16 canvas->drawColor(SK_ColorWHITE);
17 SkFont font(fontMgr->matchFamilyStyle(nullptr, {}), 12);
18
19 // BW Dither
20 canvas->translate(5, 5);
21 SkPaint p;
22 p.setShader(make_bw_dither());
23 canvas->drawRect({0, 0, 100, 100}, p);
24 SkPaint black;
25 canvas->drawString("BW Dither", 0, 125, font, black);
26
27 // Scaled BW Dither
28 canvas->translate(105, 0);
29 canvas->save();
30 canvas->scale(0.5, 0.5);
31 canvas->drawRect({0, 0, 200, 200}, p);
32 canvas->restore();
33 canvas->drawString("Scaled Dither", 0, 125, font, black);
34
35 // Blend black on to white
36 canvas->translate(105, 0);
37 p.setColor(0x80000000);
38 p.setShader(nullptr);
39 canvas->drawRect({0, 0, 100, 100}, p);
40 p.setShader(nullptr);
41 canvas->drawString("Blend", 0, 125, font, black);
42
43 // Opaque color (0xFFBCBCBC)
44 canvas->translate(105, 0);
45 p.setColor(0xFFBCBCBC);
46 canvas->drawRect({0, 0, 100, 100}, p);
47 canvas->drawString("0xFFBCBCBC", 0, 125, font, black);
48
49 // Opaque color (0xFF808080)
50 canvas->translate(105, 0);
51 p.setColor(0xFF808080);
52 canvas->drawRect({0, 0, 100, 100}, p);
53 canvas->drawString("0xFF808080", 0, 125, font, black);
54}
55} // END FIDDLE
REG_FIDDLE_SRGB(50_percent_gray, 530, 150, false, 0, 0, false)
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
sk_sp< SkTypeface > matchFamilyStyle(const char familyName[], const SkFontStyle &) const
Definition: SkFontMgr.cpp:109
Definition: SkFont.h:35
sk_sp< SkFontMgr > fontMgr
Definition: examples.cpp:32
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
static SkImageInfo MakeN32Premul(int width, int height)