/ src / Ryujinx.Graphics.Texture / Utils / RgbaColor8.cs
RgbaColor8.cs
 1  using System;
 2  using System.Runtime.CompilerServices;
 3  using System.Runtime.Intrinsics;
 4  using System.Runtime.Intrinsics.X86;
 5  
 6  namespace Ryujinx.Graphics.Texture.Utils
 7  {
 8      struct RgbaColor8 : IEquatable<RgbaColor8>
 9      {
10          public byte R;
11          public byte G;
12          public byte B;
13          public byte A;
14  
15          public RgbaColor8(byte r, byte g, byte b, byte a)
16          {
17              R = r;
18              G = g;
19              B = b;
20              A = a;
21          }
22  
23          public static RgbaColor8 FromUInt32(uint color)
24          {
25              return Unsafe.As<uint, RgbaColor8>(ref color);
26          }
27  
28          public static bool operator ==(RgbaColor8 x, RgbaColor8 y)
29          {
30              return x.Equals(y);
31          }
32  
33          public static bool operator !=(RgbaColor8 x, RgbaColor8 y)
34          {
35              return !x.Equals(y);
36          }
37  
38          [MethodImpl(MethodImplOptions.AggressiveInlining)]
39          public RgbaColor32 GetColor32()
40          {
41              if (Sse41.IsSupported)
42              {
43                  Vector128<byte> color = Vector128.CreateScalarUnsafe(Unsafe.As<RgbaColor8, uint>(ref this)).AsByte();
44                  return new RgbaColor32(Sse41.ConvertToVector128Int32(color));
45              }
46              else
47              {
48                  return new RgbaColor32(R, G, B, A);
49              }
50          }
51  
52          public uint ToUInt32()
53          {
54              return Unsafe.As<RgbaColor8, uint>(ref this);
55          }
56  
57          public readonly override int GetHashCode()
58          {
59              return HashCode.Combine(R, G, B, A);
60          }
61  
62          public readonly override bool Equals(object obj)
63          {
64              return obj is RgbaColor8 other && Equals(other);
65          }
66  
67          public readonly bool Equals(RgbaColor8 other)
68          {
69              return R == other.R && G == other.G && B == other.B && A == other.A;
70          }
71  
72          public readonly byte GetComponent(int index)
73          {
74              return index switch
75              {
76                  0 => R,
77                  1 => G,
78                  2 => B,
79                  3 => A,
80                  _ => throw new ArgumentOutOfRangeException(nameof(index)),
81              };
82          }
83      }
84  }