PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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