highcontrastfilter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkColorFilter.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkShader.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTileMode.h"
  22. #include "include/core/SkTypeface.h"
  23. #include "include/core/SkTypes.h"
  24. #include "include/effects/SkGradientShader.h"
  25. #include "include/effects/SkHighContrastFilter.h"
  26. #include "tools/ToolUtils.h"
  27. #include <stdio.h>
  28. #include <string.h>
  29. using InvertStyle = SkHighContrastConfig::InvertStyle;
  30. static SkScalar kSize = 200;
  31. static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
  32. static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
  33. static void draw_label(SkCanvas* canvas, const SkHighContrastConfig& config) {
  34. char labelBuffer[256];
  35. const char* invertStr =
  36. (config.fInvertStyle == InvertStyle::kInvertBrightness ?
  37. "InvBrightness" :
  38. (config.fInvertStyle == InvertStyle::kInvertLightness ?
  39. "InvLightness" : "NoInvert"));
  40. snprintf(labelBuffer, sizeof(labelBuffer), "%s%s contrast=%.1f",
  41. config.fGrayscale ? "Gray " : "",
  42. invertStr,
  43. config.fContrast);
  44. SkFont font;
  45. font.setTypeface(ToolUtils::create_portable_typeface());
  46. font.setSize(0.05f);
  47. font.setEdging(SkFont::Edging::kAlias);
  48. size_t len = strlen(labelBuffer);
  49. SkScalar width = font.measureText(labelBuffer, len, SkTextEncoding::kUTF8);
  50. canvas->drawSimpleText(labelBuffer, len, SkTextEncoding::kUTF8, 0.5f - width / 2, 0.16f, font, SkPaint());
  51. }
  52. static void draw_scene(SkCanvas* canvas, const SkHighContrastConfig& config) {
  53. SkRect bounds = SkRect::MakeLTRB(0.0f, 0.0f, 1.0f, 1.0f);
  54. SkPaint xferPaint;
  55. xferPaint.setColorFilter(SkHighContrastFilter::Make(config));
  56. canvas->saveLayer(&bounds, &xferPaint);
  57. SkPaint paint;
  58. bounds = SkRect::MakeLTRB(0.1f, 0.2f, 0.9f, 0.4f);
  59. paint.setARGB(0xff, 0x66, 0x11, 0x11);
  60. canvas->drawRect(bounds, paint);
  61. SkFont font;
  62. font.setSize(0.15f);
  63. font.setEdging(SkFont::Edging::kAlias);
  64. paint.setARGB(0xff, 0xbb, 0x77, 0x77);
  65. canvas->drawString("A", 0.15f, 0.35f, font, paint);
  66. bounds = SkRect::MakeLTRB(0.1f, 0.8f, 0.9f, 1.0f);
  67. paint.setARGB(0xff, 0xcc, 0xcc, 0xff);
  68. canvas->drawRect(bounds, paint);
  69. paint.setARGB(0xff, 0x88, 0x88, 0xbb);
  70. canvas->drawString("Z", 0.75f, 0.95f, font, paint);
  71. bounds = SkRect::MakeLTRB(0.1f, 0.4f, 0.9f, 0.6f);
  72. SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
  73. SkColor colors[] = { SK_ColorWHITE, SK_ColorBLACK };
  74. SkScalar pos[] = { 0.2f, 0.8f };
  75. paint.setShader(SkGradientShader::MakeLinear(
  76. pts, colors, pos,
  77. SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
  78. canvas->drawRect(bounds, paint);
  79. bounds = SkRect::MakeLTRB(0.1f, 0.6f, 0.9f, 0.8f);
  80. SkColor colors2[] = { SK_ColorGREEN, SK_ColorWHITE };
  81. paint.setShader(SkGradientShader::MakeLinear(
  82. pts, colors2, pos,
  83. SK_ARRAY_COUNT(colors2), SkTileMode::kClamp));
  84. canvas->drawRect(bounds, paint);
  85. canvas->restore();
  86. }
  87. class HighContrastFilterGM : public skiagm::GM {
  88. public:
  89. HighContrastFilterGM() {
  90. SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
  91. SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
  92. SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
  93. SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
  94. SkScalar pos[] = { 0.2f, 1.0f };
  95. SkHighContrastConfig fConfig;
  96. fFilter = SkHighContrastFilter::Make(fConfig);
  97. fGr1 = SkGradientShader::MakeLinear(
  98. g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
  99. SkTileMode::kClamp);
  100. fGr2 = SkGradientShader::MakeLinear(
  101. g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
  102. SkTileMode::kClamp);
  103. }
  104. protected:
  105. SkString onShortName() override {
  106. return SkString("highcontrastfilter");
  107. }
  108. SkISize onISize() override {
  109. return SkISize::Make(600, 420);
  110. }
  111. void onDraw(SkCanvas* canvas) override {
  112. SkHighContrastConfig configs[] = {
  113. { false, InvertStyle::kNoInvert, 0.0f },
  114. { false, InvertStyle::kInvertBrightness, 0.0f },
  115. { false, InvertStyle::kInvertLightness, 0.0f },
  116. { false, InvertStyle::kInvertLightness, 0.2f },
  117. { true, InvertStyle::kNoInvert, 0.0f },
  118. { true, InvertStyle::kInvertBrightness, 0.0f },
  119. { true, InvertStyle::kInvertLightness, 0.0f },
  120. { true, InvertStyle::kInvertLightness, 0.2f },
  121. };
  122. for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
  123. SkScalar x = kSize * (i % 4);
  124. SkScalar y = kSize * (i / 4);
  125. canvas->save();
  126. canvas->translate(x, y);
  127. canvas->scale(kSize, kSize);
  128. draw_scene(canvas, configs[i]);
  129. draw_label(canvas, configs[i]);
  130. canvas->restore();
  131. }
  132. }
  133. private:
  134. sk_sp<SkColorFilter> fFilter;
  135. sk_sp<SkShader> fGr1, fGr2;
  136. typedef skiagm::GM INHERITED;
  137. };
  138. DEF_GM(return new HighContrastFilterGM;)