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