1 /* 2 * Copyright 2007 The Android Open Source Project 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 "SkColorSpaceXformer.h" 12 #include "SkShaderBase.h" 13 14 /** \class SkColorShader 15 A Shader that represents a single color. In general, this effect can be 16 accomplished by just using the color field on the paint, but if an 17 actual shader object is needed, this provides that feature. 18 */ 19 class SkColorShader : public SkShaderBase { 20 public: 21 /** Create a ColorShader that ignores the color in the paint, and uses the 22 specified color. Note: like all shaders, at draw time the paint's alpha 23 will be respected, and is applied to the specified color. 24 */ 25 explicit SkColorShader(SkColor c); 26 27 bool isOpaque() const override; 28 bool isConstant() const override { return true; } 29 30 GradientType asAGradient(GradientInfo* info) const override; 31 32 #if SK_SUPPORT_GPU 33 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; 34 #endif 35 36 private: 37 SK_FLATTENABLE_HOOKS(SkColorShader) 38 39 void flatten(SkWriteBuffer&) const override; 40 41 bool onAsLuminanceColor(SkColor* lum) const override { 42 *lum = fColor; 43 return true; 44 } 45 46 bool onAppendStages(const StageRec&) const override; 47 48 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override { 49 return SkShader::MakeColorShader(xformer->apply(fColor)); 50 } 51 52 SkColor fColor; 53 }; 54 55 class SkColor4Shader : public SkShaderBase { 56 public: 57 SkColor4Shader(const SkColor4f&, sk_sp<SkColorSpace>); 58 59 bool isOpaque() const override { return fColor.isOpaque(); } 60 bool isConstant() const override { return true; } 61 62 #if SK_SUPPORT_GPU 63 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; 64 #endif 65 66 private: 67 SK_FLATTENABLE_HOOKS(SkColor4Shader) 68 69 void flatten(SkWriteBuffer&) const override; 70 bool onAppendStages(const StageRec&) const override; 71 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override; 72 73 sk_sp<SkColorSpace> fColorSpace; 74 const SkColor4f fColor; 75 }; 76 77 #endif 78