color4f.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2011 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/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkColorFilter.h"
  12. #include "include/core/SkColorSpace.h"
  13. #include "include/core/SkImageInfo.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkSurface.h"
  19. #include "include/effects/SkColorMatrix.h"
  20. static sk_sp<SkShader> make_opaque_color() {
  21. return SkShaders::Color(0xFFFF0000);
  22. }
  23. static sk_sp<SkShader> make_alpha_color() {
  24. return SkShaders::Color(0x80FF0000);
  25. }
  26. static sk_sp<SkColorFilter> make_cf_null() {
  27. return nullptr;
  28. }
  29. static sk_sp<SkColorFilter> make_cf0() {
  30. SkColorMatrix cm;
  31. cm.setSaturation(0.75f);
  32. return SkColorFilters::Matrix(cm);
  33. }
  34. static sk_sp<SkColorFilter> make_cf1() {
  35. SkColorMatrix cm;
  36. cm.setSaturation(0.75f);
  37. auto a = SkColorFilters::Matrix(cm);
  38. // CreateComposedFilter will try to concat these two matrices, resulting in a single
  39. // filter (which is good for speed). For this test, we want to force a real compose of
  40. // these two, so our inner filter has a scale-up, which disables the optimization of
  41. // combining the two matrices.
  42. cm.setScale(1.1f, 0.9f, 1);
  43. return a->makeComposed(SkColorFilters::Matrix(cm));
  44. }
  45. static sk_sp<SkColorFilter> make_cf2() {
  46. return SkColorFilters::Blend(0x8044CC88, SkBlendMode::kSrcATop);
  47. }
  48. static void draw_into_canvas(SkCanvas* canvas) {
  49. const SkRect r = SkRect::MakeWH(50, 100);
  50. sk_sp<SkShader> (*shaders[])() { make_opaque_color, make_alpha_color };
  51. sk_sp<SkColorFilter> (*filters[])() { make_cf_null, make_cf0, make_cf1, make_cf2 };
  52. SkPaint paint;
  53. for (auto shProc : shaders) {
  54. paint.setShader(shProc());
  55. for (auto cfProc : filters) {
  56. paint.setColorFilter(cfProc());
  57. canvas->drawRect(r, paint);
  58. canvas->translate(60, 0);
  59. }
  60. }
  61. }
  62. DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
  63. canvas->translate(10, 10);
  64. SkPaint bg;
  65. // need the target to be opaque, so we can draw it to the screen
  66. // even if it holds sRGB values.
  67. bg.setColor(0xFFFFFFFF);
  68. sk_sp<SkColorSpace> colorSpaces[]{
  69. nullptr,
  70. SkColorSpace::MakeSRGB()
  71. };
  72. for (auto colorSpace : colorSpaces) {
  73. const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
  74. colorSpace);
  75. auto surface(SkSurface::MakeRaster(info));
  76. surface->getCanvas()->drawPaint(bg);
  77. draw_into_canvas(surface->getCanvas());
  78. surface->draw(canvas, 0, 0, nullptr);
  79. canvas->translate(0, 120);
  80. }
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////
  83. DEF_SIMPLE_GM(color4shader, canvas, 360, 480) {
  84. canvas->translate(10, 10);
  85. auto srgb = SkColorSpace::MakeSRGB();
  86. auto spin = srgb->makeColorSpin(); // RGB -> GBR
  87. const SkColor4f colors[] {
  88. { 1, 0, 0, 1 },
  89. { 0, 1, 0, 1 },
  90. { 0, 0, 1, 1 },
  91. { 0.5, 0.5, 0.5, 1 },
  92. };
  93. SkPaint paint;
  94. SkRect r = SkRect::MakeWH(100, 100);
  95. for (const auto& c4 : colors) {
  96. sk_sp<SkShader> shaders[] {
  97. SkShaders::Color(c4, nullptr),
  98. SkShaders::Color(c4, srgb),
  99. SkShaders::Color(c4, spin),
  100. };
  101. canvas->save();
  102. for (const auto& s : shaders) {
  103. paint.setShader(s);
  104. canvas->drawRect(r, paint);
  105. canvas->translate(r.width() * 6 / 5, 0);
  106. }
  107. canvas->restore();
  108. canvas->translate(0, r.height() * 6 / 5);
  109. }
  110. }