xref: /aosp_15_r20/external/skia/include/effects/SkHighContrastFilter.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2 * Copyright 2017 Google Inc.
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 SkHighContrastFilter_DEFINED
9 #define SkHighContrastFilter_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkTypes.h"
14 
15 class SkColorFilter;
16 
17 /**
18  *  Configuration struct for SkHighContrastFilter.
19  *
20  *  Provides transformations to improve contrast for users with low vision.
21  */
22 struct SkHighContrastConfig {
23     enum class InvertStyle {
24         kNoInvert,
25         kInvertBrightness,
26         kInvertLightness,
27 
28         kLast = kInvertLightness
29     };
30 
SkHighContrastConfigSkHighContrastConfig31     SkHighContrastConfig() {
32         fGrayscale = false;
33         fInvertStyle = InvertStyle::kNoInvert;
34         fContrast = 0.0f;
35     }
36 
SkHighContrastConfigSkHighContrastConfig37     SkHighContrastConfig(bool grayscale,
38                          InvertStyle invertStyle,
39                          SkScalar contrast)
40         : fGrayscale(grayscale)
41         , fInvertStyle(invertStyle)
42         , fContrast(contrast) {}
43 
44     // Returns true if all of the fields are set within the valid range.
isValidSkHighContrastConfig45     bool isValid() const {
46         return fInvertStyle >= InvertStyle::kNoInvert &&
47                fInvertStyle <= InvertStyle::kInvertLightness &&
48                fContrast >= -1.0 &&
49                fContrast <= 1.0;
50     }
51 
52     // If true, the color will be converted to grayscale.
53     bool fGrayscale;
54 
55     // Whether to invert brightness, lightness, or neither.
56     InvertStyle fInvertStyle;
57 
58     // After grayscale and inverting, the contrast can be adjusted linearly.
59     // The valid range is -1.0 through 1.0, where 0.0 is no adjustment.
60     SkScalar  fContrast;
61 };
62 
63 /**
64  *  Color filter that provides transformations to improve contrast
65  *  for users with low vision.
66  *
67  *  Applies the following transformations in this order. Each of these
68  *  can be configured using SkHighContrastConfig.
69  *
70  *    - Conversion to grayscale
71  *    - Color inversion (either in RGB or HSL space)
72  *    - Increasing the resulting contrast.
73  *
74  * Calling SkHighContrastFilter::Make will return nullptr if the config is
75  * not valid, e.g. if you try to call it with a contrast outside the range of
76  * -1.0 to 1.0.
77  */
78 
79 struct SK_API SkHighContrastFilter {
80     // Returns the filter, or nullptr if the config is invalid.
81     static sk_sp<SkColorFilter> Make(const SkHighContrastConfig& config);
82 };
83 
84 #endif
85