aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Texture/Utils/RgbaColor8.cs
blob: 91535c1ae692db1d40c885f977ff388d572d76c2 (plain) (blame)
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
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace Ryujinx.Graphics.Texture.Utils
{
    struct RgbaColor8 : IEquatable<RgbaColor8>
    {
        public byte R;
        public byte G;
        public byte B;
        public byte A;

        public RgbaColor8(byte r, byte g, byte b, byte a)
        {
            R = r;
            G = g;
            B = b;
            A = a;
        }

        public static RgbaColor8 FromUInt32(uint color)
        {
            return Unsafe.As<uint, RgbaColor8>(ref color);
        }

        public static bool operator ==(RgbaColor8 x, RgbaColor8 y)
        {
            return x.Equals(y);
        }

        public static bool operator !=(RgbaColor8 x, RgbaColor8 y)
        {
            return !x.Equals(y);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public RgbaColor32 GetColor32()
        {
            if (Sse41.IsSupported)
            {
                Vector128<byte> color = Vector128.CreateScalarUnsafe(Unsafe.As<RgbaColor8, uint>(ref this)).AsByte();
                return new RgbaColor32(Sse41.ConvertToVector128Int32(color));
            }
            else
            {
                return new RgbaColor32(R, G, B, A);
            }
        }

        public uint ToUInt32()
        {
            return Unsafe.As<RgbaColor8, uint>(ref this);
        }

        public readonly override int GetHashCode()
        {
            return HashCode.Combine(R, G, B, A);
        }

        public readonly override bool Equals(object obj)
        {
            return obj is RgbaColor8 other && Equals(other);
        }

        public readonly bool Equals(RgbaColor8 other)
        {
            return R == other.R && G == other.G && B == other.B && A == other.A;
        }

        public readonly byte GetComponent(int index)
        {
            return index switch
            {
                0 => R,
                1 => G,
                2 => B,
                3 => A,
                _ => throw new ArgumentOutOfRangeException(nameof(index)),
            };
        }
    }
}