1// Aseprite UI Library
2// Copyright (C) 2022 Igara Studio S.A.
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "ui/paint.h"
12
13#include "base/exception.h"
14
15#if LAF_SKIA
16 #include "include/core/SkBitmap.h"
17#endif
18
19namespace ui {
20
21void set_checkered_paint_mode(Paint& paint,
22 const int param,
23 const gfx::Color a,
24 const gfx::Color b)
25{
26#if LAF_SKIA
27 SkPaint& skPaint = paint.skPaint();
28 skPaint.setBlendMode(SkBlendMode::kSrcOver);
29
30 SkBitmap bitmap;
31 if (!bitmap.tryAllocPixels(
32 SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType))) {
33 throw base::Exception("Cannot create temporary Skia surface");
34 }
35
36 {
37 SkPMColor A = SkPreMultiplyARGB(gfx::geta(a), gfx::getr(a), gfx::getg(a), gfx::getb(a));
38 SkPMColor B = SkPreMultiplyARGB(gfx::geta(b), gfx::getr(b), gfx::getg(b), gfx::getb(b));
39 int offset = 7 - (param & 7);
40 for (int y=0; y<8; y++)
41 for (int x=0; x<8; x++)
42 *bitmap.getAddr32(x, y) = (((x+y+offset)&7) < 4 ? B: A);
43 }
44
45 sk_sp<SkShader> shader(
46 bitmap.makeShader(SkTileMode::kRepeat,
47 SkTileMode::kRepeat,
48 SkSamplingOptions()));
49 skPaint.setShader(shader);
50#endif
51}
52
53} // namespace ui
54