1/*
2 * Copyright 2006 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 SkColorFilter_DEFINED
9#define SkColorFilter_DEFINED
10
11#include "include/core/SkBlendMode.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkFlattenable.h"
14
15class SkColorMatrix;
16
17/**
18* ColorFilters are optional objects in the drawing pipeline. When present in
19* a paint, they are called with the "src" colors, and return new colors, which
20* are then passed onto the next stage (either ImageFilter or Xfermode).
21*
22* All subclasses are required to be reentrant-safe : it must be legal to share
23* the same instance between several threads.
24*/
25class SK_API SkColorFilter : public SkFlattenable {
26public:
27 // DEPRECATED. skbug.com/8941
28
29 bool asColorMode(SkColor* color, SkBlendMode* mode) const;
30
31 /** If the filter can be represented by a source color plus Mode, this
32 * returns true, and sets (if not NULL) the color and mode appropriately.
33 * If not, this returns false and ignores the parameters.
34 */
35 bool asAColorMode(SkColor* color, SkBlendMode* mode) const;
36
37 /** If the filter can be represented by a 5x4 matrix, this
38 * returns true, and sets the matrix appropriately.
39 * If not, this returns false and ignores the parameter.
40 */
41 bool asAColorMatrix(float matrix[20]) const;
42
43 // deprecated, use isAlphaUnchanged()
44 enum Flags {
45 kAlphaUnchanged_Flag = 1 << 0,
46 };
47 uint32_t getFlags() const;
48
49
50 // Returns true if the filter is guaranteed to never change the alpha of a color it filters.
51 bool isAlphaUnchanged() const;
52
53 SkColor filterColor(SkColor) const;
54
55 /**
56 * Converts the src color (in src colorspace), into the dst colorspace,
57 * then applies this filter to it, returning the filtered color in the dst colorspace.
58 */
59 SkColor4f filterColor4f(const SkColor4f& srcColor, SkColorSpace* srcCS,
60 SkColorSpace* dstCS) const;
61
62 /** Construct a colorfilter whose effect is to first apply the inner filter and then apply
63 * this filter, applied to the output of the inner filter.
64 *
65 * result = this(inner(...))
66 */
67 sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter> inner) const;
68
69 static SkFlattenable::Type GetFlattenableType() {
70 return kSkColorFilter_Type;
71 }
72
73private:
74 SkColorFilter() = default;
75 friend class SkColorFilterBase;
76
77 typedef SkFlattenable INHERITED;
78};
79
80class SK_API SkColorFilters {
81public:
82 static sk_sp<SkColorFilter> Compose(sk_sp<SkColorFilter> outer, sk_sp<SkColorFilter> inner) {
83 return outer ? outer->makeComposed(inner) : inner;
84 }
85 static sk_sp<SkColorFilter> Blend(SkColor c, SkBlendMode mode);
86 static sk_sp<SkColorFilter> Matrix(const SkColorMatrix&);
87 static sk_sp<SkColorFilter> Matrix(const float rowMajor[20]);
88
89 // A version of Matrix which operates in HSLA space instead of RGBA.
90 // I.e. HSLA-to-RGBA(Matrix(RGBA-to-HSLA(input))).
91 static sk_sp<SkColorFilter> HSLAMatrix(const float rowMajor[20]);
92
93 static sk_sp<SkColorFilter> LinearToSRGBGamma();
94 static sk_sp<SkColorFilter> SRGBToLinearGamma();
95 static sk_sp<SkColorFilter> Lerp(float t, sk_sp<SkColorFilter> dst, sk_sp<SkColorFilter> src);
96
97private:
98 SkColorFilters() = delete;
99};
100
101#endif
102