/graphics/gradient.d

http://github.com/wilkie/djehuty · D · 145 lines · 90 code · 23 blank · 32 comment · 12 complexity · fedc2a54d175a598935948bb8a245bac MD5 · raw file

  1. /*
  2. * gradient.d
  3. *
  4. * This module implements an object that defines a gradient.
  5. *
  6. * Author: Dave Wilkinson
  7. * Originated: December 19th, 2009
  8. *
  9. */
  10. module graphics.gradient;
  11. import core.definitions;
  12. import core.color;
  13. import core.variant;
  14. import io.console;
  15. // Description: This class represents a linear gradient.
  16. class Gradient {
  17. // Description: This will create a default linear gradient.
  18. this() {
  19. _angle = 3.14159265;
  20. _width = 50;
  21. _origx = 0;
  22. _origy = 0;
  23. }
  24. // Description: This will create a linear gradient with a particular width.
  25. // width: The length of the gradient.
  26. this(double width) {
  27. _angle = 3.14159265;
  28. _width = width;
  29. _origx = 0;
  30. _origy = 0;
  31. }
  32. // Description: This will create a linear gradient with a particular width at an angle.
  33. // x0: The horizontal origin of the gradient.
  34. // y0: The vertical origin of the gradient.
  35. // width: The length of the gradient.
  36. // angle: The angle at which the gradient is drawn.
  37. this(double x0, double y0, double width, double angle, ...) {
  38. _width = width;
  39. _origx = x0;
  40. _origy = y0;
  41. this.angle = angle;
  42. Variadic vars = new Variadic(_arguments, _argptr);
  43. float pt;
  44. foreach(i, item; vars) {
  45. if (i % 2 == 0) {
  46. pt = item.to!(double);
  47. }
  48. else {
  49. Color clr = item.to!(Color);
  50. add(pt, clr);
  51. }
  52. }
  53. }
  54. // Description: This will add a point to the gradient and attach a color. Colors are
  55. // interpolated from 0.0 to 1.0 based upon the values given by this function.
  56. // Or it will replace the value if it already exists.
  57. // point: The placement of the point within the width of the gradient. Possible values
  58. // range from 0.0 to 1.0.
  59. // color: The color that will represent this point. Defaults to black.
  60. void add(double point, Color color = Color.Black) {
  61. if (point < 0.0) {
  62. point = 0.0;
  63. }
  64. if (point > 1.0) {
  65. point = 1.0;
  66. }
  67. foreach(size_t i, pt; _points) {
  68. if (pt == point) {
  69. _clrs[i] = color;
  70. return;
  71. }
  72. }
  73. _points ~= point;
  74. _clrs ~= color;
  75. }
  76. void remove(double point) {
  77. if (point < 0.0) {
  78. return;
  79. }
  80. if (point > 1.0) {
  81. return;
  82. }
  83. foreach(size_t i, pt; _points) {
  84. if (pt == point) {
  85. _points = _points[0..i] ~ _points[i+1..$];
  86. _clrs = _clrs[0..i] ~ _clrs[i+1..$];
  87. return;
  88. }
  89. }
  90. }
  91. // Description: This will return the current angle represented by this gradient.
  92. // Returns: The angle in radians.
  93. double angle() {
  94. return _angle;
  95. }
  96. // Description: This will set the angle for the gradient.
  97. // value: The new angle in radians.
  98. void angle(double value) {
  99. if (value !<> double.infinity) {
  100. value = 0.0;
  101. }
  102. value %= 2*3.14159265;
  103. _angle = value;
  104. }
  105. // Description: This will return the list of colors.
  106. // Returns: An array of Color objects the represent this gradient.
  107. Color[] colors() {
  108. return _clrs.dup;
  109. }
  110. // Description: This will return the list of points.
  111. // Returns: An array of points where possible values range from 0.0 to 1.0.
  112. double[] points() {
  113. return _points.dup;
  114. }
  115. package:
  116. double _origx;
  117. double _origy;
  118. double _angle;
  119. double _width;
  120. double[] _points;
  121. Color[] _clrs;
  122. }