-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradientColorPicker.cpp
More file actions
73 lines (67 loc) · 2.25 KB
/
Copy pathgradientColorPicker.cpp
File metadata and controls
73 lines (67 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdlib.h>
#include "gradientColorPicker.h"
/**
* Constructs a new gradientColorPicker.
*
* @param fadeColor1 The first color to start the gradient at.
* @param fadeColor2 The second color to end the gradient with.
* @param radius How quickly to transition to fadeColor2.
* @param centerX X coordinate for the center of the gradient.
* @param centerY Y coordinate for the center of the gradient.
*/
gradientColorPicker::gradientColorPicker( RGBAPixel fadeColor1,
RGBAPixel fadeColor2, int radius, int centerX, int centerY ) {
/**
* @todo Construct your gradientColorPicker here! You may find it
* helpful to create additional member variables to store things.
*/
color1 = fadeColor1;
color2 = fadeColor2;
rad = radius;
center_x = centerX;
center_y = centerY;
}
/**
* Picks the color for pixel (x, y).
*
* The first color fades into the second color as you move from the initial
* fill point, the center, to the radius. Beyond the radius, all pixels
* should be just color2.
*
* You should calculate the distance between two points using the standard
* Manhattan distance formula,
*
* \f$d = |center\_x - given\_x| + |center\_y - given\_y|\f$
*
* Then, scale each of the three channels (R, G, and B) from fadeColor1 to
* fadeColor2 linearly from d = 0 to d = radius.
*
* For example, the red color at distance d where d is less than the radius
* must be
*
* \f$ redFill = fadeColor1.red - \left\lfloor
\frac{d*fadeColor1.red}{radius}\right\rfloor +
\left\lfloor\frac{d*fadeColor2.red}{radius}\right\rfloor\f$
*
* Note that all values are integers. If you do not follow this formula,
* your colors may be very close but round differently than ours.
*
* @param x The x coordinate to pick a color for.
* @param y The y coordinate to pick a color for.
* @return The color selected for (x, y).
*/
RGBAPixel gradientColorPicker::operator()(int x, int y)
{
RGBAPixel color;
/**
* @todo Return the correct color here!
*/
int d;
d = abs(center_x - x) + abs(center_y - y);
if(d>= rad)
d= rad;
color.red = color1.red - (d*color1.red)/rad + (d*color2.red)/rad;
color.blue = color1.blue - (d*color1.blue)/rad + (d*color2.blue)/rad;
color.green = color1.green - (d*color1.green)/rad + (d*color2.green)/rad;
return color;
}