1 | // LAF OS 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 | #ifndef OS_SKIA_PAINT_H_INCLUDED |
8 | #define OS_SKIA_PAINT_H_INCLUDED |
9 | #pragma once |
10 | |
11 | #include "gfx/color.h" |
12 | |
13 | #include "include/core/SkPaint.h" |
14 | |
15 | namespace os { |
16 | |
17 | // SkPaint wrapper, information how to paint a shape/primitive in a |
18 | // canvas (stroke, fill or both; stroke width; color, etc.). |
19 | class Paint : public PaintBase { |
20 | public: |
21 | bool antialias() const { return m_skPaint.isAntiAlias(); } |
22 | void antialias(const bool state) { |
23 | m_skPaint.setAntiAlias(state); |
24 | } |
25 | |
26 | Style style() const { |
27 | return static_cast<Style>(m_skPaint.getStyle()); |
28 | } |
29 | void style(const Style style) { |
30 | m_skPaint.setStyle(static_cast<SkPaint::Style>(style)); |
31 | } |
32 | |
33 | gfx::Color color() const { |
34 | const SkColor c = m_skPaint.getColor(); |
35 | return gfx::rgba(SkColorGetR(c), |
36 | SkColorGetG(c), |
37 | SkColorGetB(c), |
38 | SkColorGetA(c)); |
39 | } |
40 | void color(const gfx::Color c) { |
41 | m_skPaint.setColor(SkColorSetARGB(gfx::geta(c), |
42 | gfx::getr(c), |
43 | gfx::getg(c), |
44 | gfx::getb(c))); |
45 | } |
46 | |
47 | float strokeWidth() const { return m_skPaint.getStrokeWidth(); } |
48 | void strokeWidth(const float strokeWidth) { |
49 | return m_skPaint.setStrokeWidth(strokeWidth); |
50 | } |
51 | |
52 | BlendMode blendMode() const { |
53 | auto bm = m_skPaint.asBlendMode(); |
54 | if (bm.has_value()) { return static_cast<BlendMode>(*bm); } |
55 | else return BlendMode::Src; |
56 | } |
57 | void blendMode(const BlendMode blendMode) { |
58 | m_skPaint.setBlendMode(static_cast<SkBlendMode>(blendMode)); |
59 | } |
60 | |
61 | const SkPaint& skPaint() const { return m_skPaint; } |
62 | SkPaint& skPaint() { return m_skPaint; } |
63 | |
64 | private: |
65 | SkPaint m_skPaint; |
66 | }; |
67 | |
68 | static_assert((int)SkPaint::kFill_Style == (int)Paint::Fill && |
69 | (int)SkPaint::kStroke_Style == (int)Paint::Stroke && |
70 | (int)SkPaint::kStrokeAndFill_Style == (int)Paint::StrokeAndFill, |
71 | "Paint styles don't match with Skia" ); |
72 | |
73 | static_assert((int)SkBlendMode::kClear == (int)BlendMode::Clear && |
74 | (int)SkBlendMode::kSrc == (int)BlendMode::Src && |
75 | (int)SkBlendMode::kDst == (int)BlendMode::Dst && |
76 | (int)SkBlendMode::kSrcOver == (int)BlendMode::SrcOver && |
77 | (int)SkBlendMode::kDstOver == (int)BlendMode::DstOver && |
78 | (int)SkBlendMode::kSrcIn == (int)BlendMode::SrcIn && |
79 | (int)SkBlendMode::kDstIn == (int)BlendMode::DstIn && |
80 | (int)SkBlendMode::kSrcOut == (int)BlendMode::SrcOut && |
81 | (int)SkBlendMode::kDstOut == (int)BlendMode::DstOut && |
82 | (int)SkBlendMode::kSrcATop == (int)BlendMode::SrcATop && |
83 | (int)SkBlendMode::kDstATop == (int)BlendMode::DstATop && |
84 | (int)SkBlendMode::kXor == (int)BlendMode::Xor && |
85 | (int)SkBlendMode::kPlus == (int)BlendMode::Plus && |
86 | (int)SkBlendMode::kModulate == (int)BlendMode::Modulate && |
87 | (int)SkBlendMode::kScreen == (int)BlendMode::Screen && |
88 | (int)SkBlendMode::kLastCoeffMode == (int)BlendMode::LastCoeffMode && |
89 | (int)SkBlendMode::kOverlay == (int)BlendMode::Overlay && |
90 | (int)SkBlendMode::kDarken == (int)BlendMode::Darken && |
91 | (int)SkBlendMode::kLighten == (int)BlendMode::Lighten && |
92 | (int)SkBlendMode::kColorDodge == (int)BlendMode::ColorDodge && |
93 | (int)SkBlendMode::kColorBurn == (int)BlendMode::ColorBurn && |
94 | (int)SkBlendMode::kHardLight == (int)BlendMode::HardLight && |
95 | (int)SkBlendMode::kSoftLight == (int)BlendMode::SoftLight && |
96 | (int)SkBlendMode::kDifference == (int)BlendMode::Difference && |
97 | (int)SkBlendMode::kExclusion == (int)BlendMode::Exclusion && |
98 | (int)SkBlendMode::kMultiply == (int)BlendMode::Multiply && |
99 | (int)SkBlendMode::kLastSeparableMode == (int)BlendMode::LastSeparableMode && |
100 | (int)SkBlendMode::kHue == (int)BlendMode::Hue && |
101 | (int)SkBlendMode::kSaturation == (int)BlendMode::Saturation && |
102 | (int)SkBlendMode::kColor == (int)BlendMode::Color && |
103 | (int)SkBlendMode::kLuminosity == (int)BlendMode::Luminosity && |
104 | (int)SkBlendMode::kLastMode == (int)BlendMode::LastMode, |
105 | "Blend modes don't match with Skia" ); |
106 | |
107 | } // namespace os |
108 | |
109 | #endif |
110 | |