| 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 |
*/ |
| 29 |
explicit SkColorShader(const SkColor4f& c) : fColor(c) {} |
| 30 |
|
| 31 |
bool isOpaque() const override { return fColor.isOpaque(); } |
| 32 |
bool isConstant(SkColor4f* color = nullptr) const override { |
| 33 |
if (color) { |
| 34 |
*color = fColor; |
| 35 |
} |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
ShaderType type() const override { return ShaderType::kColor; } |
| 40 |
|
| 41 |
const SkColor4f& color() const { return fColor; } |
| 42 |
|
| 43 |
private: |
| 44 |
friend void ::SkRegisterColorShaderFlattenable(); |
| 45 |
SK_FLATTENABLE_HOOKS(SkColorShader) |
| 46 |
|
| 47 |
void flatten(SkWriteBuffer&) const override; |
| 48 |
|
| 49 |
bool onAsLuminanceColor(SkColor4f* lum) const override { |
| 50 |
*lum = fColor; |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; |
| 55 |
|
| 56 |
// The color is stored in extended sRGB, regardless of the original color space that was |
| 57 |
// passed into SkShaders::Color(). |
| 58 |
const SkColor4f fColor; |
| 59 |
}; |
| 60 |
|
| 61 |
#endif |