-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathColor32.cs
More file actions
152 lines (134 loc) · 5.69 KB
/
Copy pathColor32.cs
File metadata and controls
152 lines (134 loc) · 5.69 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
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine.Scripting;
using UnityEngine.Bindings;
namespace UnityEngine
{
// Representation of RGBA colors in 32 bit format
[UsedByNativeCode]
[StructLayout(LayoutKind.Explicit)]
[Serializable]
public partial struct Color32 : IEquatable<Color32>, IFormattable
{
// An rgba int has been unioned with the four r g b a bytes.
// This ensures the struct is 4 byte aligned for webgl.
[FieldOffset(0)]
[Ignore(DoesNotContributeToSize = true)]
private int rgba;
// Red component of the color.
[FieldOffset(0)]
public byte r;
// Green component of the color.
[FieldOffset(1)]
public byte g;
// Blue component of the color.
[FieldOffset(2)]
public byte b;
// Alpha component of the color.
[FieldOffset(3)]
public byte a;
// Constructs a new Color with given r, g, b, a components.
public Color32(byte r, byte g, byte b, byte a)
{
rgba = 0; this.r = r; this.g = g; this.b = b; this.a = a;
}
// Color32 can be implicitly converted to and from [[Color]].
public static implicit operator Color32(Color c) => new Color32() {
r = (byte)(Mathf.Round((Mathf.Clamp01(c.r) * 255f))),
g = (byte)(Mathf.Round((Mathf.Clamp01(c.g) * 255f))),
b = (byte)(Mathf.Round((Mathf.Clamp01(c.b) * 255f))),
a = (byte)(Mathf.Round((Mathf.Clamp01(c.a) * 255f)))
};
// Color32 can be implicitly converted to and from [[Color]].
public static implicit operator Color(Color32 c) => new Color() { r = c.r / 255f, g = c.g / 255f, b = c.b / 255f, a = c.a / 255f };
// Interpolates between colors /a/ and /b/ by /t/.
public static Color32 Lerp(Color32 a, Color32 b, float t)
{
t = Mathf.Clamp01(t);
return new Color32() {
r = (byte)(a.r + (b.r - a.r) * t),
g = (byte)(a.g + (b.g - a.g) * t),
b = (byte)(a.b + (b.b - a.b) * t),
a = (byte)(a.a + (b.a - a.a) * t)
};
}
// Interpolates between colors /a/ and /b/ by /t/.
public static Color32 Lerp(in Color32 a, in Color32 b, float t)
{
t = Mathf.Clamp01(t);
return new Color32() {
r = (byte)(a.r + (b.r - a.r) * t),
g = (byte)(a.g + (b.g - a.g) * t),
b = (byte)(a.b + (b.b - a.b) * t),
a = (byte)(a.a + (b.a - a.a) * t)
};
}
// Interpolates between colors /a/ and /b/ by /t/ without clamping the interpolant
public static Color32 LerpUnclamped(Color32 a, Color32 b, float t) => new Color32() {
r = (byte)(a.r + (b.r - a.r) * t),
g = (byte)(a.g + (b.g - a.g) * t),
b = (byte)(a.b + (b.b - a.b) * t),
a = (byte)(a.a + (b.a - a.a) * t)
};
// Interpolates between colors /a/ and /b/ by /t/ without clamping the interpolant
public static Color32 LerpUnclamped(in Color32 a, in Color32 b, float t) => new Color32() {
r = (byte)(a.r + (b.r - a.r) * t),
g = (byte)(a.g + (b.g - a.g) * t),
b = (byte)(a.b + (b.b - a.b) * t),
a = (byte)(a.a + (b.a - a.a) * t)
};
// Access the r, g, b,a components using [0], [1], [2], [3] respectively.
public byte this[int index]
{
readonly get
{
switch (index)
{
case 0: return r;
case 1: return g;
case 2: return b;
case 3: return a;
default:
throw new IndexOutOfRangeException("Invalid Color32 index(" + index + ")!");
}
}
set
{
switch (index)
{
case 0: r = value; break;
case 1: g = value; break;
case 2: b = value; break;
case 3: a = value; break;
default:
throw new IndexOutOfRangeException("Invalid Color32 index(" + index + ")!");
}
}
}
public override readonly int GetHashCode() => rgba.GetHashCode();
public override readonly bool Equals(object other)
{
if (other is Color32 color)
return Equals(in color);
return false;
}
public readonly bool Equals(Color32 other) => rgba == other.rgba;
public readonly bool Equals(in Color32 other) => rgba == other.rgba;
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public override readonly string ToString() => ToString(null, null);
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public readonly string ToString(string format) => ToString(format, null);
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public readonly string ToString(string format, IFormatProvider formatProvider)
{
if (formatProvider == null)
formatProvider = CultureInfo.InvariantCulture.NumberFormat;
return string.Format("RGBA({0}, {1}, {2}, {3})", r.ToString(format, formatProvider), g.ToString(format, formatProvider), b.ToString(format, formatProvider), a.ToString(format, formatProvider));
}
}
} //namespace