forked from ricardojmendez/LibNoise.Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradient.cs
executable file
·196 lines (172 loc) · 6.82 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Graphics;
using UnityEngine;
namespace LibNoise.Unity
{
/// <summary>
/// Provides a color gradient.
/// </summary>
public struct Gradient
{
#region Fields
private List<KeyValuePair<double, Color>> m_data;
private bool m_inverted;
private static Gradient _empty;
private static Gradient _terrain;
private static Gradient _grayscale;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of Gradient.
/// </summary>
static Gradient()
{
Gradient._terrain.m_data = new List<KeyValuePair<double, Color>>();
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(-1.0, new Color(0, 0, 128)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(-0.2, new Color(0.125f, 0.25f, 0.5f)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(-0.04, new Color(0.25f, 0.375f, 0.75f)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(-0.02, new Color(0.75f, 0.75f, 0.5f)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(0.0, new Color(0, 0.75f, 0)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(0.25, new Color(0.75f, 0.75f, 0)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(0.5, new Color(0.625f, 0.375f, 0.25f)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(0.75, new Color(0.5f, 1, 1)));
Gradient._terrain.m_data.Add(new KeyValuePair<double, Color>(1.0, Color.white));
Gradient._terrain.m_inverted = false;
Gradient._grayscale.m_data = new List<KeyValuePair<double, Color>>();
Gradient._grayscale.m_data.Add(new KeyValuePair<double, Color>(-1.0, Color.black));
Gradient._grayscale.m_data.Add(new KeyValuePair<double, Color>(1.0, Color.white));
Gradient._grayscale.m_inverted = false;
Gradient._empty.m_data = new List<KeyValuePair<double, Color>>();
Gradient._empty.m_data.Add(new KeyValuePair<double, Color>(-1.0, Color.clear));
Gradient._empty.m_data.Add(new KeyValuePair<double, Color>(1.0, Color.clear));
Gradient._empty.m_inverted = false;
}
/// <summary>
/// Initializes a new instance of Gradient.
/// </summary>
public Gradient(Color color)
{
this.m_data = new List<KeyValuePair<double, Color>>();
this.m_data.Add(new KeyValuePair<double, Color>(-1.0, color));
this.m_data.Add(new KeyValuePair<double, Color>(1.0, color));
this.m_inverted = false;
}
/// <summary>
/// Initializes a new instance of Gradient.
/// </summary>
public Gradient(Color start, Color end)
{
this.m_data = new List<KeyValuePair<double, Color>>();
this.m_data.Add(new KeyValuePair<double, Color>(-1.0, start));
this.m_data.Add(new KeyValuePair<double, Color>(1.0, end));
this.m_inverted = false;
}
#endregion
#region Indexers
/// <summary>
/// Gets or sets a gradient step by its position.
/// </summary>
/// <param name="position">The position of the gradient step.</param>
/// <returns>The corresponding color value.</returns>
public Color this[double position]
{
get
{
int i = 0;
for (i = 0; i < this.m_data.Count; i++)
{
if (position < this.m_data[i].Key)
{
break;
}
}
int i0 = (int)Mathf.Clamp(i - 1, 0, this.m_data.Count - 1);
int i1 = (int)Mathf.Clamp(i, 0, this.m_data.Count - 1);
if (i0 == i1)
{
return this.m_data[i1].Value;
}
double ip0 = this.m_data[i0].Key;
double ip1 = this.m_data[i1].Key;
double a = (position - ip0) / (ip1 - ip0);
if (this.m_inverted)
{
a = 1.0 - a;
double t = ip0;
ip0 = ip1;
ip1 = t;
}
return Color.Lerp(this.m_data[i0].Value, this.m_data[i1].Value, (float)a);
}
set
{
for (int i = 0; i < this.m_data.Count; i++)
{
if (this.m_data[i].Key == position)
{
this.m_data.RemoveAt(i);
break;
}
}
this.m_data.Add(new KeyValuePair<double, Color>(position, value));
this.m_data.Sort(delegate(KeyValuePair<double, Color> lhs, KeyValuePair<double, Color> rhs)
{ return lhs.Key.CompareTo(rhs.Key); });
}
}
#endregion
#region Properties
/// <summary>
/// Gets or sets a value whether the gradient is inverted.
/// </summary>
public bool IsInverted
{
get { return this.m_inverted; }
set { this.m_inverted = value; }
}
/// <summary>
/// Gets the empty instance of Gradient.
/// </summary>
public static Gradient Empty
{
get { return Gradient._empty; }
}
/// <summary>
/// Gets the grayscale instance of Gradient.
/// </summary>
public static Gradient Grayscale
{
get { return Gradient._grayscale; }
}
/// <summary>
/// Gets the terrain instance of Gradient.
/// </summary>
public static Gradient Terrain
{
get { return Gradient._terrain; }
}
#endregion
#region Methods
/// <summary>
/// Clears the gradient to transparent black.
/// </summary>
public void Clear()
{
this.m_data.Clear();
this.m_data.Add(new KeyValuePair<double, Color>(0.0, Color.clear));
this.m_data.Add(new KeyValuePair<double, Color>(1.0, Color.clear));
}
/// <summary>
/// Inverts the gradient.
/// </summary>
public void Invert()
{
// UNDONE: public void Invert()
throw new NotImplementedException();
}
#endregion
}
}