colorfilters.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2014 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/SkPaint.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkShader.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTileMode.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/effects/SkColorMatrixFilter.h"
  21. #include "include/effects/SkGradientShader.h"
  22. static sk_sp<SkShader> make_shader(const SkRect& bounds) {
  23. const SkPoint pts[] = {
  24. { bounds.left(), bounds.top() },
  25. { bounds.right(), bounds.bottom() },
  26. };
  27. const SkColor colors[] = {
  28. SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK,
  29. SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW,
  30. };
  31. return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  32. SkTileMode::kClamp);
  33. }
  34. typedef void (*InstallPaint)(SkPaint*, uint32_t, uint32_t);
  35. static void install_nothing(SkPaint* paint, uint32_t, uint32_t) {
  36. paint->setColorFilter(nullptr);
  37. }
  38. static void install_lighting(SkPaint* paint, uint32_t mul, uint32_t add) {
  39. paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(mul, add));
  40. }
  41. class ColorFiltersGM : public skiagm::GM {
  42. SkString onShortName() override { return SkString("lightingcolorfilter"); }
  43. SkISize onISize() override { return {620, 430}; }
  44. void onDraw(SkCanvas* canvas) override {
  45. SkRect r = {0, 0, 600, 50};
  46. SkPaint paint;
  47. paint.setShader(make_shader(r));
  48. const struct {
  49. InstallPaint fProc;
  50. uint32_t fData0, fData1;
  51. } rec[] = {
  52. { install_nothing, 0, 0 },
  53. { install_lighting, 0xFF0000, 0 },
  54. { install_lighting, 0x00FF00, 0 },
  55. { install_lighting, 0x0000FF, 0 },
  56. { install_lighting, 0x000000, 0xFF0000 },
  57. { install_lighting, 0x000000, 0x00FF00 },
  58. { install_lighting, 0x000000, 0x0000FF },
  59. };
  60. canvas->translate(10, 10);
  61. for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
  62. rec[i].fProc(&paint, rec[i].fData0, rec[i].fData1);
  63. canvas->drawRect(r, paint);
  64. canvas->translate(0, r.height() + 10);
  65. }
  66. }
  67. };
  68. DEF_GM(return new ColorFiltersGM;)