• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Google LLC
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 #ifndef SkColorShader_DEFINED
9 #define SkColorShader_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFlattenable.h"
13 #include "src/shaders/SkShaderBase.h"
14 
15 class SkReadBuffer;
16 class SkWriteBuffer;
17 struct SkStageRec;
18 
19 /** \class SkColorShader
20     A Shader that represents a single color. In general, this effect can be accomplished by just
21     using the color field on the paint, but if an actual shader object is needed, this provides that
22     feature.  Note: like all shaders, at draw time the paint's alpha will be respected, and is
23     applied to the specified color.
24 */
25 class SkColorShader : public SkShaderBase {
26 public:
27     /** Create a ColorShader wrapping the given sRGB color.
28     */
SkColorShader(const SkColor4f & c)29     explicit SkColorShader(const SkColor4f& c) : fColor(c) {}
30 
isOpaque()31     bool isOpaque() const override { return fColor.isOpaque(); }
isConstant()32     bool isConstant() const override { return true; }
33 
type()34     ShaderType type() const override { return ShaderType::kColor; }
35 
color()36     const SkColor4f& color() const { return fColor; }
37 
38 private:
39     friend void ::SkRegisterColorShaderFlattenable();
40     SK_FLATTENABLE_HOOKS(SkColorShader)
41 
42     void flatten(SkWriteBuffer&) const override;
43 
onAsLuminanceColor(SkColor4f * lum)44     bool onAsLuminanceColor(SkColor4f* lum) const override {
45         *lum = fColor;
46         return true;
47     }
48 
49     bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override;
50 
51     // The color is stored in extended sRGB, regardless of the original color space that was
52     // passed into SkShaders::Color().
53     const SkColor4f fColor;
54 };
55 
56 #endif
57