~groovestomp/gsnes

gsnes/color.c -rwxr-xr-x 3.3 KiB
9de07668 — GrooveStomp Foreground rendering works 1 year, 4 months ago
                                                                                
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
/******************************************************************************
  GrooveStomp's NES Emulator
  Copyright (c) 2019 Aaron Oman (GrooveStomp)

  File: color.c
  Created: 2019-08-15
  Updated: 2019-11-15
  Author: Aaron Oman
  Notice: GNU GPLv3 License

  This program comes with ABSOLUTELY NO WARRANTY.
  This is free software, and you are welcome to redistribute it under certain
  conditions; See LICENSE for details.
 ******************************************************************************/
//! \file color.c

#include "color.h"

struct color ColorWhite = { 0xFFFFFFFF };
struct color ColorBlack = { 0x000000FF };
struct color ColorRed = { 0xFF0000FF };
struct color ColorGreen = { 0x00FF00FF };
struct color ColorBlue = { 0x0000FFFF };
struct color ColorPurple = { 0x7F00FFFF };
struct color ColorYellow = { 0xFFFF00FF };
struct color ColorCyan = { 0x00FFFFFF };
struct color ColorPink = { 0xFF00FFFF };
struct color ColorGray = { 0x888888FF };

void ColorSetInt(struct color *color, char component, unsigned int value) {
        unsigned int pos = 0;
        switch (component) {
                case 'r':
                        pos = 3;
                        break;
                case 'g':
                        pos = 2;
                        break;
                case 'b':
                        pos = 1;
                        break;
                case 'a':
                        pos = 0;
                        break;
                default:
                        pos = 0;
        }

        unsigned int shift = pos * 8;

        unsigned int rgba = color->rgba & ~(0xFF << shift);
        color->rgba = rgba | (value << shift);
}

void ColorSetFloat(struct color *color, char component, float value) {
        unsigned int intVal = (unsigned int)(value * 255.0f);
        ColorSetInt(color, component, intVal);
}

struct color ColorInitFloats(float r, float g, float b, float a) {
        struct color color = { 0 };

        ColorSetFloat(&color, 'r', r);
        ColorSetFloat(&color, 'g', g);
        ColorSetFloat(&color, 'b', b);
        ColorSetFloat(&color, 'a', a);

        return color;
}

unsigned int ColorGetInt(struct color color, char component) {
        unsigned int pos = 0;
        switch (component) {
                case 'r':
                        pos = 3;
                        break;
                case 'g':
                        pos = 2;
                        break;
                case 'b':
                        pos = 1;
                        break;
                case 'a':
                        pos = 0;
                        break;
                default:
                        pos = 0;
        }

        unsigned int shift = pos * 8;
        return (color.rgba >> shift) & 0xFF;
}

float ColorGetFloat(struct color color, char component) {
        int value = ColorGetInt(color, component);
        return (float)value / 255.0f;
}

struct color ColorInitInts(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
        struct color color = { 0 };

        ColorSetInt(&color, 'r', r);
        ColorSetInt(&color, 'g', g);
        ColorSetInt(&color, 'b', b);
        ColorSetInt(&color, 'a', a);

        return color;
}

struct color ColorInitInt(uint32_t rgba) {
        struct color color = { 0 };
        color.rgba = rgba;
        return color;
}