colorwheel.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/SkFont.h"
  11. #include "include/core/SkFontStyle.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkTypeface.h"
  17. #include "include/core/SkTypes.h"
  18. #include "tools/Resources.h"
  19. #include "tools/ToolUtils.h"
  20. static void draw_image(SkCanvas* canvas, const char* resource, int x, int y) {
  21. sk_sp<SkImage> image(GetResourceAsImage(resource));
  22. if (image) {
  23. canvas->drawImage(image, SkIntToScalar(x), SkIntToScalar(y));
  24. } else {
  25. SkDebugf("\nCould not decode file '%s'. Did you forget"
  26. " to set the resourcePath?\n", resource);
  27. }
  28. }
  29. /*
  30. This GM tests whether the image decoders properly decode each color
  31. channel. Four copies of the same image should appear in the GM, and
  32. the letter R should be red, B should be blue, G green, C cyan, M
  33. magenta, Y yellow, and K black. In all but the JPEG version of the
  34. image, the letters should be on a white disc on a transparent
  35. background (rendered as a checkerboard). The JPEG image has a grey
  36. background and compression artifacts.
  37. */
  38. DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) {
  39. ToolUtils::draw_checkerboard(canvas);
  40. draw_image(canvas, "images/color_wheel.png", 0, 0); // top left
  41. draw_image(canvas, "images/color_wheel.gif", 128, 0); // top right
  42. draw_image(canvas, "images/color_wheel.webp", 0, 128); // bottom left
  43. draw_image(canvas, "images/color_wheel.jpg", 128, 128); // bottom right
  44. }
  45. DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) {
  46. SkFont font(ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold()), 18);
  47. font.setEdging(SkFont::Edging::kAlias);
  48. canvas->clear(SK_ColorLTGRAY);
  49. canvas->drawString("R", 8.0f, 20.0f, font, SkPaint(SkColors::kRed));
  50. canvas->drawString("G", 24.0f, 20.0f, font, SkPaint(SkColors::kGreen));
  51. canvas->drawString("B", 40.0f, 20.0f, font, SkPaint(SkColors::kBlue));
  52. canvas->drawString("C", 56.0f, 20.0f, font, SkPaint(SkColors::kCyan));
  53. canvas->drawString("M", 72.0f, 20.0f, font, SkPaint(SkColors::kMagenta));
  54. canvas->drawString("Y", 88.0f, 20.0f, font, SkPaint(SkColors::kYellow));
  55. canvas->drawString("K", 104.0f, 20.0f, font, SkPaint(SkColors::kBlack));
  56. }