mesh-pattern-conical.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright © 2009 Adrian Johnson
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: Adrian Johnson <ajohnson@redneon.com>
  25. */
  26. #include "cairo-test.h"
  27. #include <math.h>
  28. #define PAT_WIDTH 100
  29. #define PAT_HEIGHT 100
  30. #define SIZE PAT_WIDTH
  31. #define PAD 2
  32. #define WIDTH (PAD + SIZE + PAD)
  33. #define HEIGHT WIDTH
  34. /*
  35. * This test is designed to paint a mesh pattern which contains 8
  36. * circular sectors approximating a conical gradient.
  37. */
  38. #define CENTER_X 50
  39. #define CENTER_Y 50
  40. #define RADIUS 50
  41. static void
  42. sector_patch (cairo_pattern_t *pattern,
  43. double angle_A,
  44. double A_r, double A_g, double A_b,
  45. double angle_B,
  46. double B_r, double B_g, double B_b)
  47. {
  48. double r_sin_A, r_cos_A;
  49. double r_sin_B, r_cos_B;
  50. double h;
  51. r_sin_A = RADIUS * sin (angle_A);
  52. r_cos_A = RADIUS * cos (angle_A);
  53. r_sin_B = RADIUS * sin (angle_B);
  54. r_cos_B = RADIUS * cos (angle_B);
  55. h = 4.0/3.0 * tan ((angle_B - angle_A) / 4.0);
  56. cairo_mesh_pattern_begin_patch (pattern);
  57. cairo_mesh_pattern_move_to (pattern, CENTER_X, CENTER_Y);
  58. cairo_mesh_pattern_line_to (pattern,
  59. CENTER_X + r_cos_A,
  60. CENTER_Y + r_sin_A);
  61. cairo_mesh_pattern_curve_to (pattern,
  62. CENTER_X + r_cos_A - h * r_sin_A,
  63. CENTER_Y + r_sin_A + h * r_cos_A,
  64. CENTER_X + r_cos_B + h * r_sin_B,
  65. CENTER_Y + r_sin_B - h * r_cos_B,
  66. CENTER_X + r_cos_B,
  67. CENTER_Y + r_sin_B);
  68. cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 1, 1);
  69. cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, A_r, A_g, A_b);
  70. cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, B_r, B_g, B_b);
  71. cairo_mesh_pattern_end_patch (pattern);
  72. }
  73. static cairo_test_status_t
  74. draw (cairo_t *cr, int width, int height)
  75. {
  76. cairo_pattern_t *pattern;
  77. cairo_set_source_rgb (cr, 1, 1, 1);
  78. cairo_paint (cr);
  79. cairo_translate (cr, PAD, PAD);
  80. pattern = cairo_pattern_create_mesh ();
  81. sector_patch (pattern,
  82. 0, 1, 0, 0,
  83. M_PI/4, 1, 1, 0);
  84. sector_patch (pattern,
  85. M_PI/4, 0, 1, 0,
  86. M_PI/2, 0, 1, 1);
  87. sector_patch (pattern,
  88. M_PI/2, 0, 0, 1,
  89. 3*M_PI/4, 1, 0, 1);
  90. sector_patch (pattern,
  91. 3*M_PI/4, 1, 0, 0,
  92. M_PI, 1, 1, 0);
  93. sector_patch (pattern,
  94. -M_PI, 1, 1, 0,
  95. -3*M_PI/4, 0, 1, 0);
  96. sector_patch (pattern,
  97. -3*M_PI/4, 0, 1, 0,
  98. -M_PI/2, 0, 1, 1);
  99. sector_patch (pattern,
  100. -M_PI/2, 0, 1, 1,
  101. -M_PI/4, 0, 0, 1);
  102. sector_patch (pattern,
  103. -M_PI/4, 0, 0, 1,
  104. 0, 1, 0, 0);
  105. cairo_set_source (cr, pattern);
  106. cairo_paint (cr);
  107. cairo_pattern_destroy (pattern);
  108. return CAIRO_TEST_SUCCESS;
  109. }
  110. CAIRO_TEST (mesh_pattern_conical,
  111. "Paint a conical pattern using a mesh pattern",
  112. "conical, mesh, pattern", /* keywords */
  113. NULL, /* requirements */
  114. WIDTH, HEIGHT,
  115. NULL, draw)