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/SkColorSpace.h" 13 #include "include/core/SkFlattenable.h" 14 #include "include/core/SkRefCnt.h" 15 #include "src/shaders/SkShaderBase.h" 16 17 class SkReadBuffer; 18 class SkWriteBuffer; 19 struct SkStageRec; 20 21 /** \class SkColorShader 22 A Shader that represents a single color. In general, this effect can be 23 accomplished by just using the color field on the paint, but if an 24 actual shader object is needed, this provides that feature. 25 */ 26 class SkColorShader : public SkShaderBase { 27 public: 28 /** Create a ColorShader that ignores the color in the paint, and uses the 29 specified color. Note: like all shaders, at draw time the paint's alpha 30 will be respected, and is applied to the specified color. 31 */ 32 explicit SkColorShader(SkColor c); 33 34 bool isOpaque() const override; isConstant()35 bool isConstant() const override { return true; } 36 type()37 ShaderType type() const override { return ShaderType::kColor; } 38 color()39 SkColor color() const { return fColor; } 40 41 private: 42 friend void ::SkRegisterColorShaderFlattenable(); 43 SK_FLATTENABLE_HOOKS(SkColorShader) 44 45 void flatten(SkWriteBuffer&) const override; 46 onAsLuminanceColor(SkColor4f * lum)47 bool onAsLuminanceColor(SkColor4f* lum) const override { 48 *lum = SkColor4f::FromColor(fColor); 49 return true; 50 } 51 52 bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; 53 54 SkColor fColor; 55 }; 56 57 class SkColor4Shader : public SkShaderBase { 58 public: 59 SkColor4Shader(const SkColor4f&, sk_sp<SkColorSpace>); 60 isOpaque()61 bool isOpaque() const override { return fColor.isOpaque(); } isConstant()62 bool isConstant() const override { return true; } 63 type()64 ShaderType type() const override { return ShaderType::kColor4; } 65 colorSpace()66 sk_sp<SkColorSpace> colorSpace() const { return fColorSpace; } color()67 SkColor4f color() const { return fColor; } 68 69 private: 70 friend void ::SkRegisterColor4ShaderFlattenable(); 71 SK_FLATTENABLE_HOOKS(SkColor4Shader) 72 73 void flatten(SkWriteBuffer&) const override; 74 bool onAsLuminanceColor(SkColor4f* lum) const override; 75 bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; 76 77 sk_sp<SkColorSpace> fColorSpace; 78 const SkColor4f fColor; 79 }; 80 81 #endif 82