Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Color.cs
浏览该文件的文档.
1namespace Engine {
2 public struct Color : IEquatable<Color> {
3 public uint PackedValue;
4
5 public static Color Transparent = default;
6
7 public static Color Black = new(0, 0, 0, 255);
8
9 public static Color DarkGray = new(64, 64, 64, 255);
10
11 public static Color Gray = new(128, 128, 128, 255);
12
13 public static Color LightGray = new(192, 192, 192, 255);
14
15 public static Color White = new(255, 255, 255, 255);
16
17 public static Color Red = new(255, 0, 0, 255);
18
19 public static Color Green = new(0, 255, 0, 255);
20
21 public static Color Yellow = new(255, 255, 0, 255);
22
23 public static Color Blue = new(0, 0, 255, 255);
24
25 public static Color Magenta = new(255, 0, 255, 255);
26
27 public static Color Cyan = new(0, 255, 255, 255);
28
29 public static Color DarkRed = new(128, 0, 0, 255);
30
31 public static Color DarkGreen = new(0, 128, 0, 255);
32
33 public static Color DarkYellow = new(128, 128, 0, 255);
34
35 public static Color DarkBlue = new(0, 0, 128, 255);
36
37 public static Color DarkMagenta = new(128, 0, 128, 255);
38
39 public static Color DarkCyan = new(0, 128, 128, 255);
40
41 public static Color LightRed = new(255, 128, 128, 255);
42
43 public static Color LightGreen = new(128, 255, 128, 255);
44
45 public static Color LightYellow = new(255, 255, 128, 255);
46
47 public static Color LightBlue = new(128, 128, 255, 255);
48
49 public static Color LightMagenta = new(255, 128, 255, 255);
50
51 public static Color LightCyan = new(128, 255, 255, 255);
52
53 public static Color Orange = new(255, 128, 0, 255);
54
55 public static Color Pink = new(255, 0, 128, 255);
56
57 public static Color Chartreuse = new(128, 255, 0, 255);
58
59 public static Color Violet = new(128, 0, 255, 255);
60
61 public static Color MintGreen = new(0, 255, 128, 255);
62
63 public static Color SkyBlue = new(0, 128, 255, 255);
64
65 public static Color Brown = new(128, 64, 0, 255);
66
67 public static Color Purple = new(128, 0, 64, 255);
68
69 public static Color Olive = new(64, 128, 0, 255);
70
71 public static Color Indigo = new(64, 0, 128, 255);
72
73 public static Color MutedGreen = new(0, 128, 64, 255);
74
75 public static Color InkBlue = new(0, 64, 128, 255);
76
77 public byte R {
78 get => (byte)PackedValue;
79 set => PackedValue = (uint)(((int)PackedValue & -256) | value);
80 }
81
82 public byte G {
83 get => (byte)(PackedValue >> 8);
84 set => PackedValue = (uint)(((int)PackedValue & -65281) | (value << 8));
85 }
86
87 public byte B {
88 get => (byte)(PackedValue >> 16);
89 set => PackedValue = (uint)(((int)PackedValue & -16711681) | (value << 16));
90 }
91
92 public byte A {
93 get => (byte)(PackedValue >> 24);
94 set => PackedValue = (uint)((int)(PackedValue & 0xFFFFFF) | (value << 24));
95 }
96
97 public Color RGB {
98 get => new((uint)((int)PackedValue | -16777216));
99 set => PackedValue = (uint)(((int)PackedValue & -16777216) | (int)(value.PackedValue & 0xFFFFFF));
100 }
101
102 public Color(uint packedValue) => PackedValue = packedValue;
103
104 public Color(byte r, byte g, byte b) => PackedValue = (uint)(-16777216 | (b << 16) | (g << 8) | r);
105
106 public Color(byte r, byte g, byte b, byte a) => PackedValue = (uint)((a << 24) | (b << 16) | (g << 8) | r);
107
108 public Color(int r, int g, int b, int a) => this = new Color(
109 (byte)Math.Clamp(r, 0, 255),
110 (byte)Math.Clamp(g, 0, 255),
111 (byte)Math.Clamp(b, 0, 255),
112 (byte)Math.Clamp(a, 0, 255)
113 );
114
115 public Color(int r, int g, int b) => this = new Color(r, g, b, 255);
116
117 public Color(float r, float g, float b, float a) => this = new Color(
118 (byte)(MathUtils.Saturate(r) * 255f),
119 (byte)(MathUtils.Saturate(g) * 255f),
120 (byte)(MathUtils.Saturate(b) * 255f),
121 (byte)(MathUtils.Saturate(a) * 255f)
122 );
123
124 public Color(float r, float g, float b) => this = new Color(
125 (byte)(MathUtils.Saturate(r) * 255f),
126 (byte)(MathUtils.Saturate(g) * 255f),
127 (byte)(MathUtils.Saturate(b) * 255f),
128 byte.MaxValue
129 );
130
131 public Color(Color rgb, byte a) {
133 A = a;
134 }
135
136 public Color(Color rgb, int a) => this = new Color(rgb, (byte)Math.Clamp(a, 0, 255));
137
138 public Color(Color rgb, float a) => this = new Color(rgb, (byte)(MathUtils.Saturate(a) * 255f));
139
140 public Color(Vector4 rgba) => this = new Color(rgba.X, rgba.Y, rgba.Z, rgba.W);
141
142 public Color(Vector3 rgb) => this = new Color(rgb.X, rgb.Y, rgb.Z);
143
144 public static implicit operator Color((byte R, byte G, byte B) v) => new(v.R, v.G, v.B);
145
146 public static implicit operator Color((byte R, byte G, byte B, byte A) v) => new(v.R, v.G, v.B, v.A);
147
148 public static implicit operator Color((int R, int G, int B) v) => new(v.R, v.G, v.B);
149
150 public static implicit operator Color((int R, int G, int B, int A) v) => new(v.R, v.G, v.B, v.A);
151
152 public static implicit operator Color((float R, float G, float B, float A) v) => new(v.R, v.G, v.B, v.A);
153
154 public override int GetHashCode() => (int)PackedValue;
155
156 public override bool Equals(object obj) => obj is Color && Equals((Color)obj);
157
158 public override string ToString() => $"{R},{G},{B},{A}";
159
160 public bool Equals(Color other) => PackedValue == other.PackedValue;
161
162 public static Color Lerp(Color c1, Color c2, float f) => new(
163 (int)MathUtils.Lerp(c1.R, c2.R, f),
164 (int)MathUtils.Lerp(c1.G, c2.G, f),
165 (int)MathUtils.Lerp(c1.B, c2.B, f),
166 (int)MathUtils.Lerp(c1.A, c2.A, f)
167 );
168
169 public static Color LerpNotSaturated(Color c1, Color c2, float f) => new(
170 (byte)MathUtils.Lerp(c1.R, c2.R, f),
171 (byte)MathUtils.Lerp(c1.G, c2.G, f),
172 (byte)MathUtils.Lerp(c1.B, c2.B, f),
173 (byte)MathUtils.Lerp(c1.A, c2.A, f)
174 );
175
176 public static Color PremultiplyAlpha(Color c) => new((byte)(c.R * c.A / 255f), (byte)(c.G * c.A / 255f), (byte)(c.B * c.A / 255f), c.A);
177
178 public static Vector4 PremultiplyAlpha(Vector4 c) => new(c.X * c.W, c.Y * c.W, c.Z * c.W, c.W);
179
180 public static Color MultiplyAlphaOnly(Color c, float s) => new(c.R, c.G, c.B, (byte)Math.Clamp(c.A * s, 0f, 255f));
181
182 public static Color MultiplyAlphaOnlyNotSaturated(Color c, float s) => new(c.R, c.G, c.B, (byte)(c.A * s));
183
184 public static Color MultiplyColorOnly(Color c, float s) => new(
185 (byte)Math.Clamp(c.R * s, 0f, 255f),
186 (byte)Math.Clamp(c.G * s, 0f, 255f),
187 (byte)Math.Clamp(c.B * s, 0f, 255f),
188 c.A
189 );
190
191 public static Color MultiplyColorOnlyNotSaturated(Color c, float s) => new((byte)(c.R * s), (byte)(c.G * s), (byte)(c.B * s), c.A);
192
193 public static Color MultiplyColorOnlyNotSaturated(Color c, Vector3 s) => new((byte)(c.R * s.X), (byte)(c.G * s.Y), (byte)(c.B * s.Z), c.A);
194
195 public static Color MultiplyNotSaturated(Color c, float s) => new((byte)(c.R * s), (byte)(c.G * s), (byte)(c.B * s), (byte)(c.A * s));
196
197 public static Color MultiplyNotSaturated(Color c, Vector4 s) => new(
198 (byte)(c.R * s.X),
199 (byte)(c.G * s.Y),
200 (byte)(c.B * s.Z),
201 (byte)(c.A * s.W)
202 );
203
204 public static Vector3 RgbToHsv(Vector3 rgb) {
205 float num = MathUtils.Min(rgb.X, rgb.Y, rgb.Z);
206 float num2 = MathUtils.Max(rgb.X, rgb.Y, rgb.Z);
207 float z = num2;
208 float num3 = num2 - num;
209 float y;
210 float num4;
211 if (num2 != 0f) {
212 y = num3 / num2;
213 num4 = num3 == 0f ? 0f :
214 rgb.X == num2 ? (rgb.Y - rgb.Z) / num3 :
215 rgb.Y != num2 ? 4f + (rgb.X - rgb.Y) / num3 : 2f + (rgb.Z - rgb.X) / num3;
216 num4 *= 60f;
217 if (num4 < 0f) {
218 num4 += 360f;
219 }
220 return new Vector3(num4, y, z);
221 }
222 y = 0f;
223 num4 = -1f;
224 return new Vector3(num4, y, z);
225 }
226
227 public static Vector3 HsvToRgb(Vector3 hsv) {
228 if (hsv.Y == 0f) {
229 return new Vector3(hsv.Z);
230 }
231 hsv.X /= 60f;
232 int num = (int)MathF.Floor(hsv.X);
233 float num2 = hsv.X - num;
234 float num3 = hsv.Z * (1f - hsv.Y);
235 float num4 = hsv.Z * (1f - hsv.Y * num2);
236 float num5 = hsv.Z * (1f - hsv.Y * (1f - num2));
237 float x;
238 float y;
239 float z;
240 switch (num) {
241 case 0:
242 x = hsv.Z;
243 y = num5;
244 z = num3;
245 break;
246 case 1:
247 x = num4;
248 y = hsv.Z;
249 z = num3;
250 break;
251 case 2:
252 x = num3;
253 y = hsv.Z;
254 z = num5;
255 break;
256 case 3:
257 x = num3;
258 y = num4;
259 z = hsv.Z;
260 break;
261 case 4:
262 x = num5;
263 y = num3;
264 z = hsv.Z;
265 break;
266 default:
267 x = hsv.Z;
268 y = num3;
269 z = num4;
270 break;
271 }
272 return new Vector3(x, y, z);
273 }
274
275 public static bool operator ==(Color c1, Color c2) => c1.Equals(c2);
276
277 public static bool operator !=(Color c1, Color c2) => !c1.Equals(c2);
278
279 public static Color operator *(Color c, float s) => new(
280 (byte)Math.Clamp(c.R * s, 0f, 255f),
281 (byte)Math.Clamp(c.G * s, 0f, 255f),
282 (byte)Math.Clamp(c.B * s, 0f, 255f),
283 (byte)Math.Clamp(c.A * s, 0f, 255f)
284 );
285
286 public static Color operator *(float s, Color c) => new(
287 (byte)Math.Clamp(c.R * s, 0f, 255f),
288 (byte)Math.Clamp(c.G * s, 0f, 255f),
289 (byte)Math.Clamp(c.B * s, 0f, 255f),
290 (byte)Math.Clamp(c.A * s, 0f, 255f)
291 );
292
293 public static Color operator /(Color c, float s) {
294 float num = 1f / s;
295 return new Color(
296 (byte)Math.Clamp(c.R * num, 0f, 255f),
297 (byte)Math.Clamp(c.G * num, 0f, 255f),
298 (byte)Math.Clamp(c.B * num, 0f, 255f),
299 (byte)Math.Clamp(c.A * num, 0f, 255f)
300 );
301 }
302
303 public static Color operator *(Color c, Vector4 s) => new(
304 (byte)Math.Clamp(c.R * s.X, 0f, 255f),
305 (byte)Math.Clamp(c.G * s.Y, 0f, 255f),
306 (byte)Math.Clamp(c.B * s.Z, 0f, 255f),
307 (byte)Math.Clamp(c.A * s.W, 0f, 255f)
308 );
309
310 public static Color operator *(Vector4 s, Color c) => new(
311 (byte)Math.Clamp(c.R * s.X, 0f, 255f),
312 (byte)Math.Clamp(c.G * s.Y, 0f, 255f),
313 (byte)Math.Clamp(c.B * s.Z, 0f, 255f),
314 (byte)Math.Clamp(c.A * s.W, 0f, 255f)
315 );
316
317 public static Color operator +(Color c1, Color c2) => new(
318 (byte)Math.Min(c1.R + c2.R, 255),
319 (byte)Math.Min(c1.G + c2.G, 255),
320 (byte)Math.Min(c1.B + c2.B, 255),
321 (byte)Math.Min(c1.A + c2.A, 255)
322 );
323
324 public static Color operator -(Color c1, Color c2) => new(
325 (byte)Math.Max(c1.R - c2.R, 0),
326 (byte)Math.Max(c1.G - c2.G, 0),
327 (byte)Math.Max(c1.B - c2.B, 0),
328 (byte)Math.Max(c1.A - c2.A, 0)
329 );
330
331 public static Color operator *(Color c1, Color c2) => new(
332 (byte)(c1.R * c2.R / 255),
333 (byte)(c1.G * c2.G / 255),
334 (byte)(c1.B * c2.B / 255),
335 (byte)(c1.A * c2.A / 255)
336 );
337 }
338}
Engine.Color Color
Engine.Vector3 Vector3
static int Min(int x1, int x2)
static float Saturate(float x)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
static Color MultiplyAlphaOnly(Color c, float s)
static Color Cyan
static Color MultiplyAlphaOnlyNotSaturated(Color c, float s)
static Color Chartreuse
static Color operator/(Color c, float s)
static Color DarkMagenta
Color(int r, int g, int b)
static Color operator-(Color c1, Color c2)
static Color DarkBlue
static Color Purple
static Color Lerp(Color c1, Color c2, float f)
static bool operator==(Color c1, Color c2)
static Color DarkCyan
static Color LightCyan
static Vector4 PremultiplyAlpha(Vector4 c)
static Color Violet
static Vector3 RgbToHsv(Vector3 rgb)
static Color MutedGreen
Color(float r, float g, float b, float a)
static Color Transparent
定义 Color.cs:5
static Color LightMagenta
static Color DarkRed
static Color LightYellow
static Color DarkYellow
static Color MultiplyColorOnlyNotSaturated(Color c, Vector3 s)
Color(byte r, byte g, byte b, byte a)
static Color Red
static bool operator!=(Color c1, Color c2)
static Vector3 HsvToRgb(Vector3 hsv)
static Color White
static Color Pink
static Color LightBlue
static Color MultiplyNotSaturated(Color c, float s)
Color(byte r, byte g, byte b)
Color(Color rgb, byte a)
uint PackedValue
定义 Color.cs:3
override int GetHashCode()
static Color Magenta
static Color Brown
static Color Yellow
static Color LightGreen
static Color DarkGreen
static Color MultiplyColorOnlyNotSaturated(Color c, float s)
static Color MultiplyColorOnly(Color c, float s)
static Color Green
Color(uint packedValue)
static Color SkyBlue
static Color LightGray
override string ToString()
static Color operator+(Color c1, Color c2)
static Color LightRed
static Color PremultiplyAlpha(Color c)
Color(Color rgb, float a)
Color(Vector3 rgb)
Color(Vector4 rgba)
bool Equals(Color other)
static Color InkBlue
Color(float r, float g, float b)
static Color MultiplyNotSaturated(Color c, Vector4 s)
Color(Color rgb, int a)
static Color DarkGray
定义 Color.cs:9
static Color Orange
static Color Gray
override bool Equals(object obj)
static Color LerpNotSaturated(Color c1, Color c2, float f)
static Color Indigo
Color(int r, int g, int b, int a)
static Color MintGreen
static Color Olive
static Color operator*(Color c, float s)
static Color Blue
static Color Black
定义 Color.cs:7
static Vector3 Floor(Vector3 v)