SkHighContrastFilter.h 2.3 KB

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