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