Skip to content

Commit 239e8f2

Browse files
committed
Make Col.FormatDefaultOf more flexible
1 parent a725a1d commit 239e8f2

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

src/Aardvark.Base/Math/Colors/Color.cs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -411,41 +411,16 @@ public static long ChannelIndexNoThrow(this Format format, Channel channel)
411411
return -1;
412412
}
413413

414-
private static Dict<(Type, long), Format> s_defaultFormatMap =
415-
new Dict<(Type, long), Format>
416-
{
417-
{ (typeof(byte), 1L), Format.Gray },
418-
{ (typeof(byte), 2L), Format.NormalUV },
419-
{ (typeof(byte), 3L), Format.BGR },
420-
{ (typeof(byte), 4L), Format.BGRA },
421-
422-
{ (typeof(ushort), 1L), Format.Gray },
423-
{ (typeof(ushort), 2L), Format.NormalUV },
424-
{ (typeof(ushort), 3L), Format.RGB },
425-
{ (typeof(ushort), 4L), Format.RGBA },
426-
427-
{ (typeof(uint), 1L), Format.Gray },
428-
{ (typeof(uint), 2L), Format.NormalUV },
429-
{ (typeof(uint), 3L), Format.RGB },
430-
{ (typeof(uint), 4L), Format.RGBA },
431-
432-
{ (typeof(float), 1L), Format.Gray },
433-
{ (typeof(float), 2L), Format.NormalUV },
434-
{ (typeof(float), 3L), Format.RGB },
435-
{ (typeof(float), 4L), Format.RGBA },
436-
437-
{ (typeof(double), 1L), Format.Gray },
438-
{ (typeof(double), 2L), Format.NormalUV },
439-
{ (typeof(double), 3L), Format.RGB },
440-
{ (typeof(double), 4L), Format.RGBA },
441-
};
442-
443414
public static Format FormatDefaultOf(this Type type, long channelCount)
444415
{
445-
if (s_defaultFormatMap.TryGetValue((type, channelCount),
446-
out Format format))
447-
return format;
448-
throw new ArgumentException("no default format for this type and channel count");
416+
return channelCount switch
417+
{
418+
1 => Format.Gray,
419+
2 => Format.NormalUV,
420+
3 => (type == typeof(byte)) ? Format.BGR : Format.RGB,
421+
4 => (type == typeof(byte)) ? Format.BGRA : Format.RGBA,
422+
_ => throw new ArgumentException($"No default format for {type} with {channelCount} channels"),
423+
};
449424
}
450425

451426
#endregion

0 commit comments

Comments
 (0)