/
Gradient.cs
169 lines (151 loc) · 4.81 KB
/
Gradient.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MandelZoom
{
/// <summary>
/// Represents a color gradient for an iterative fractal.
/// </summary>
public class Gradient
{
public Gradient(double Period, Color Initial, Stop[] Stops, Color Final)
{
this.Period = Period;
this.Initial = Initial;
this.Stops = Stops;
this.Final = Final;
}
/// <summary>
/// Creates a cache of the given size for this gradient.
/// </summary>
public void CreateCache(int Size)
{
this._Cache = new Color[Size];
double d = 1.0 / (double)Size;
for (int t = 0; t < Size; t++)
{
this._Cache[t] = this._GetPeriodColor(t * d);
}
}
/// <summary>
/// Gets the color for a fragment with the given iteration count.
/// </summary>
public Color GetColor(double Iterations)
{
if (this._Cache != null)
{
double scale = this._Cache.Length / this.Period;
double index = (Iterations % this.Period) * scale;
return this._Cache[(int)index];
}
return this._GetPeriodColor(Iterations % this.Period / this.Period);
}
/// <summary>
/// Gets a color for a given offset (between 0.0 and 1.0) in a period.
/// </summary>
private Color _GetPeriodColor(double Offset)
{
Color prev = this.Initial;
double prevoffset = 0.0;
for (int t = 0; t < this.Stops.Length; t++)
{
Stop stop = this.Stops[t];
Color next = stop.Color;
double nextoffset = stop.Offset;
if (Offset < nextoffset)
return Color.Mix(prev, next, (Offset - prevoffset) / (nextoffset - prevoffset));
prev = next;
prevoffset = nextoffset;
}
return Color.Mix(prev, this.Initial, (Offset - prevoffset) / (1.0 - prevoffset));
}
/// <summary>
/// Represents a defined point in a gradient.
/// </summary>
public struct Stop
{
public Stop(double Offset, Color Color)
{
this.Offset = Offset;
this.Color = Color;
}
/// <summary>
/// The offset (relative to a period) at which this stop occurs.
/// </summary>
public double Offset;
/// <summary>
/// The color of the stop.
/// </summary>
public Color Color;
}
/// <summary>
/// The amount of iterations in a period for this gradient.
/// </summary>
public readonly double Period;
/// <summary>
/// The initial color in each period of the gradient.
/// </summary>
public readonly Color Initial;
/// <summary>
/// The stops for a period in this gradient.
/// </summary>
public readonly Stop[] Stops;
/// <summary>
/// The final color used when the iteration count is approaching the maximum iteration amount.
/// </summary>
public readonly Color Final;
/// <summary>
/// Cache for colors in the gradient.
/// </summary>
private Color[] _Cache;
}
/// <summary>
/// Represents a color.
/// </summary>
public struct Color
{
public Color(double R, double G, double B)
{
this.R = R;
this.G = G;
this.B = B;
}
/// <summary>
/// Mixes color A with color B by the given amount between 0.0 and 1.0.
/// </summary>
public static Color Mix(Color A, Color B, double Amount)
{
double af = 1.0 - Amount;
double bf = Amount;
return new Color(
A.R * af + B.R * bf,
A.G * af + B.G * bf,
A.B * af + B.B * bf);
}
/// <summary>
/// Writes the color to the given memory location.
/// </summary>
public unsafe void Write(byte* Ptr)
{
byte r = (byte)(this.R * 255.0);
byte g = (byte)(this.G * 255.0);
byte b = (byte)(this.B * 255.0);
Ptr[0] = b;
Ptr[1] = g;
Ptr[2] = r;
}
/// <summary>
/// The red component of the color.
/// </summary>
public double R;
/// <summary>
/// The green component of the color.
/// </summary>
public double G;
/// <summary>
/// The blue component of the color.
/// </summary>
public double B;
}
}