Skip to content

Commit 10750fc

Browse files
authored
Merge branch 'master' into AsSpan_Slice_parameters
2 parents c9fc5cd + 6f0e825 commit 10750fc

File tree

7 files changed

+331
-103
lines changed

7 files changed

+331
-103
lines changed

src/ImageSharp/Color/Color.Conversions.cs

Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,81 +17,201 @@ public readonly partial struct Color
1717
/// </summary>
1818
/// <param name="pixel">The <see cref="Rgba64"/> containing the color information.</param>
1919
[MethodImpl(InliningOptions.ShortMethod)]
20-
public Color(Rgba64 pixel) => this.data = pixel;
20+
public Color(Rgba64 pixel)
21+
{
22+
this.data = pixel;
23+
this.boxedHighPrecisionPixel = null;
24+
}
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="Color"/> struct.
28+
/// </summary>
29+
/// <param name="pixel">The <see cref="Rgb48"/> containing the color information.</param>
30+
[MethodImpl(InliningOptions.ShortMethod)]
31+
public Color(Rgb48 pixel)
32+
{
33+
this.data = new Rgba64(pixel.R, pixel.G, pixel.B, ushort.MaxValue);
34+
this.boxedHighPrecisionPixel = null;
35+
}
36+
37+
/// <summary>
38+
/// Initializes a new instance of the <see cref="Color"/> struct.
39+
/// </summary>
40+
/// <param name="pixel">The <see cref="La32"/> containing the color information.</param>
41+
[MethodImpl(InliningOptions.ShortMethod)]
42+
public Color(La32 pixel)
43+
{
44+
this.data = new Rgba64(pixel.L, pixel.L, pixel.L, pixel.A);
45+
this.boxedHighPrecisionPixel = null;
46+
}
47+
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="Color"/> struct.
50+
/// </summary>
51+
/// <param name="pixel">The <see cref="L16"/> containing the color information.</param>
52+
[MethodImpl(InliningOptions.ShortMethod)]
53+
public Color(L16 pixel)
54+
{
55+
this.data = new Rgba64(pixel.PackedValue, pixel.PackedValue, pixel.PackedValue, ushort.MaxValue);
56+
this.boxedHighPrecisionPixel = null;
57+
}
2158

2259
/// <summary>
2360
/// Initializes a new instance of the <see cref="Color"/> struct.
2461
/// </summary>
2562
/// <param name="pixel">The <see cref="Rgba32"/> containing the color information.</param>
2663
[MethodImpl(InliningOptions.ShortMethod)]
27-
public Color(Rgba32 pixel) => this.data = new Rgba64(pixel);
64+
public Color(Rgba32 pixel)
65+
{
66+
this.data = new Rgba64(pixel);
67+
this.boxedHighPrecisionPixel = null;
68+
}
2869

2970
/// <summary>
3071
/// Initializes a new instance of the <see cref="Color"/> struct.
3172
/// </summary>
3273
/// <param name="pixel">The <see cref="Argb32"/> containing the color information.</param>
3374
[MethodImpl(InliningOptions.ShortMethod)]
34-
public Color(Argb32 pixel) => this.data = new Rgba64(pixel);
75+
public Color(Argb32 pixel)
76+
{
77+
this.data = new Rgba64(pixel);
78+
this.boxedHighPrecisionPixel = null;
79+
}
3580

3681
/// <summary>
3782
/// Initializes a new instance of the <see cref="Color"/> struct.
3883
/// </summary>
3984
/// <param name="pixel">The <see cref="Bgra32"/> containing the color information.</param>
4085
[MethodImpl(InliningOptions.ShortMethod)]
41-
public Color(Bgra32 pixel) => this.data = new Rgba64(pixel);
86+
public Color(Bgra32 pixel)
87+
{
88+
this.data = new Rgba64(pixel);
89+
this.boxedHighPrecisionPixel = null;
90+
}
4291

4392
/// <summary>
4493
/// Initializes a new instance of the <see cref="Color"/> struct.
4594
/// </summary>
4695
/// <param name="pixel">The <see cref="Rgb24"/> containing the color information.</param>
4796
[MethodImpl(InliningOptions.ShortMethod)]
48-
public Color(Rgb24 pixel) => this.data = new Rgba64(pixel);
97+
public Color(Rgb24 pixel)
98+
{
99+
this.data = new Rgba64(pixel);
100+
this.boxedHighPrecisionPixel = null;
101+
}
49102

50103
/// <summary>
51104
/// Initializes a new instance of the <see cref="Color"/> struct.
52105
/// </summary>
53106
/// <param name="pixel">The <see cref="Bgr24"/> containing the color information.</param>
54107
[MethodImpl(InliningOptions.ShortMethod)]
55-
public Color(Bgr24 pixel) => this.data = new Rgba64(pixel);
108+
public Color(Bgr24 pixel)
109+
{
110+
this.data = new Rgba64(pixel);
111+
this.boxedHighPrecisionPixel = null;
112+
}
56113

57114
/// <summary>
58115
/// Initializes a new instance of the <see cref="Color"/> struct.
59116
/// </summary>
60117
/// <param name="vector">The <see cref="Vector4"/> containing the color information.</param>
61118
[MethodImpl(InliningOptions.ShortMethod)]
62-
public Color(Vector4 vector) => this.data = new Rgba64(vector);
119+
public Color(Vector4 vector)
120+
{
121+
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
122+
this.boxedHighPrecisionPixel = new RgbaVector(vector.X, vector.Y, vector.Z, vector.W);
123+
this.data = default;
124+
}
63125

64126
/// <summary>
65127
/// Converts a <see cref="Color"/> to <see cref="Vector4"/>.
66128
/// </summary>
67129
/// <param name="color">The <see cref="Color"/>.</param>
68130
/// <returns>The <see cref="Vector4"/>.</returns>
69-
public static explicit operator Vector4(Color color) => color.data.ToVector4();
131+
public static explicit operator Vector4(Color color) => color.ToVector4();
70132

71133
/// <summary>
72134
/// Converts an <see cref="Vector4"/> to <see cref="Color"/>.
73135
/// </summary>
74136
/// <param name="source">The <see cref="Vector4"/>.</param>
75137
/// <returns>The <see cref="Color"/>.</returns>
76138
[MethodImpl(InliningOptions.ShortMethod)]
77-
public static explicit operator Color(Vector4 source) => new Color(source);
139+
public static explicit operator Color(Vector4 source) => new(source);
78140

79141
[MethodImpl(InliningOptions.ShortMethod)]
80-
internal Rgba32 ToRgba32() => this.data.ToRgba32();
142+
internal Rgba32 ToRgba32()
143+
{
144+
if (this.boxedHighPrecisionPixel is null)
145+
{
146+
return this.data.ToRgba32();
147+
}
148+
149+
Rgba32 value = default;
150+
this.boxedHighPrecisionPixel.ToRgba32(ref value);
151+
return value;
152+
}
81153

82154
[MethodImpl(InliningOptions.ShortMethod)]
83-
internal Bgra32 ToBgra32() => this.data.ToBgra32();
155+
internal Bgra32 ToBgra32()
156+
{
157+
if (this.boxedHighPrecisionPixel is null)
158+
{
159+
return this.data.ToBgra32();
160+
}
161+
162+
Bgra32 value = default;
163+
value.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
164+
return value;
165+
}
84166

85167
[MethodImpl(InliningOptions.ShortMethod)]
86-
internal Argb32 ToArgb32() => this.data.ToArgb32();
168+
internal Argb32 ToArgb32()
169+
{
170+
if (this.boxedHighPrecisionPixel is null)
171+
{
172+
return this.data.ToArgb32();
173+
}
174+
175+
Argb32 value = default;
176+
value.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
177+
return value;
178+
}
87179

88180
[MethodImpl(InliningOptions.ShortMethod)]
89-
internal Rgb24 ToRgb24() => this.data.ToRgb24();
181+
internal Rgb24 ToRgb24()
182+
{
183+
if (this.boxedHighPrecisionPixel is null)
184+
{
185+
return this.data.ToRgb24();
186+
}
187+
188+
Rgb24 value = default;
189+
value.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
190+
return value;
191+
}
90192

91193
[MethodImpl(InliningOptions.ShortMethod)]
92-
internal Bgr24 ToBgr24() => this.data.ToBgr24();
194+
internal Bgr24 ToBgr24()
195+
{
196+
if (this.boxedHighPrecisionPixel is null)
197+
{
198+
return this.data.ToBgr24();
199+
}
200+
201+
Bgr24 value = default;
202+
value.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
203+
return value;
204+
}
93205

94206
[MethodImpl(InliningOptions.ShortMethod)]
95-
internal Vector4 ToVector4() => this.data.ToVector4();
207+
internal Vector4 ToVector4()
208+
{
209+
if (this.boxedHighPrecisionPixel is null)
210+
{
211+
return this.data.ToScaledVector4();
212+
}
213+
214+
return this.boxedHighPrecisionPixel.ToScaledVector4();
215+
}
96216
}
97217
}

0 commit comments

Comments
 (0)