testgradient.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/SkPaint.h"
  11. #include "include/core/SkPoint.h"
  12. #include "include/core/SkRRect.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkShader.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkString.h"
  17. #include "include/core/SkTileMode.h"
  18. #include "include/effects/SkGradientShader.h"
  19. class TestGradientGM : public skiagm::GM {
  20. public:
  21. TestGradientGM() {}
  22. protected:
  23. SkString onShortName() override {
  24. return SkString("testgradient");
  25. }
  26. SkISize onISize() override {
  27. return SkISize::Make(800, 800);
  28. }
  29. void onDraw(SkCanvas* canvas) override {
  30. // Set up a gradient paint for a rect.
  31. // And non-gradient paint for other objects.
  32. canvas->drawColor(SK_ColorWHITE);
  33. SkPaint paint;
  34. paint.setStyle(SkPaint::kFill_Style);
  35. paint.setAntiAlias(true);
  36. paint.setStrokeWidth(4);
  37. paint.setColor(0xFFFE938C);
  38. SkRect rect = SkRect::MakeXYWH(10, 10, 100, 160);
  39. SkPoint points[2] = {
  40. SkPoint::Make(0.0f, 0.0f),
  41. SkPoint::Make(256.0f, 256.0f)
  42. };
  43. SkColor colors[2] = {SK_ColorBLUE, SK_ColorYELLOW};
  44. SkPaint newPaint(paint);
  45. newPaint.setShader(SkGradientShader::MakeLinear(
  46. points, colors, nullptr, 2, SkTileMode::kClamp));
  47. canvas->drawRect(rect, newPaint);
  48. SkRRect oval;
  49. oval.setOval(rect);
  50. oval.offset(40, 80);
  51. paint.setColor(0xFFE6B89C);
  52. canvas->drawRRect(oval, paint);
  53. paint.setColor(0xFF9CAFB7);
  54. canvas->drawCircle(180, 50, 25, paint);
  55. rect.offset(80, 50);
  56. paint.setColor(0xFF4281A4);
  57. paint.setStyle(SkPaint::kStroke_Style);
  58. canvas->drawRoundRect(rect, 10, 10, paint);
  59. }
  60. private:
  61. typedef skiagm::GM INHERITED;
  62. };
  63. // Register the GM
  64. DEF_GM( return new TestGradientGM; )