NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2015- PPSSPP Project.
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, version 2.0 or later versions.
11
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License 2.0 for more details.
16
17// A copy of the GPL 2.0 should have been included with the program.
18// If not, see http://www.gnu.org/licenses/
19
20// Official git repository and contact information can be found at
21// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
22#pragma once
23
24#include <glm/glm.hpp>
25
27#include "NNL/utility/data.hpp"
29namespace nnl {
34namespace utl::color {
47inline u8 Convert4To8(u8 v) noexcept {
48 // Swizzle bits: 00001234 -> 12341234
49 return (v << 4) | (v);
50}
51
58inline u8 Convert5To8(u8 v) noexcept {
59 // Swizzle bits: 00012345 -> 12345123
60 return (v << 3) | (v >> 2);
61}
62
68inline u8 Convert6To8(u8 v) noexcept {
69 // Swizzle bits: 00123456 -> 12345612
70 return (v << 2) | (v >> 4);
71}
72
78inline u32 RGBA4444ToRGBA8888(u16 src) noexcept {
79 const u32 r = (src & 0x000F) << 0;
80 const u32 g = (src & 0x00F0) << 4;
81 const u32 b = (src & 0x0F00) << 8;
82 const u32 a = (src & 0xF000) << 12;
83
84 const u32 c = r | g | b | a;
85 return c | (c << 4);
86}
87
94inline u32 RGBA5551ToRGBA8888(u16 src) noexcept {
95 u8 r = Convert5To8((src >> 0) & 0x1F);
96 u8 g = Convert5To8((src >> 5) & 0x1F);
97 u8 b = Convert5To8((src >> 10) & 0x1F);
98 u8 a = (src >> 15) & 0x1;
99 a = (a) ? 0xff : 0;
100 return (a << 24) | (b << 16) | (g << 8) | r;
101}
102
109inline u32 RGB565ToRGBA8888(u16 src) noexcept {
110 u8 r = Convert5To8((src >> 0) & 0x1F);
111 u8 g = Convert6To8((src >> 5) & 0x3F);
112 u8 b = Convert5To8((src >> 11) & 0x1F);
113 u8 a = 0xFF;
114 return (a << 24) | (b << 16) | (g << 8) | r;
115}
116
122inline u16 RGBA8888ToRGB565(u32 value) noexcept {
123 u32 r = (value >> 3) & 0x1F;
124 u32 g = (value >> 5) & (0x3F << 5);
125 u32 b = (value >> 8) & (0x1F << 11);
126 return (u16)(r | g | b);
127}
128
134inline u16 RGBA8888ToRGBA5551(u32 value) noexcept {
135 u32 r = (value >> 3) & 0x1F;
136 u32 g = (value >> 6) & (0x1F << 5);
137 u32 b = (value >> 9) & (0x1F << 10);
138 u32 a = (value >> 16) & 0x8000;
139 return (u16)(r | g | b | a);
140}
141
147inline u16 RGBA8888ToRGBA4444(u32 value) noexcept {
148 const u32 c = value >> 4;
149 const u16 r = (c >> 0) & 0x000F;
150 const u16 g = (c >> 4) & 0x00F0;
151 const u16 b = (c >> 8) & 0x0F00;
152 const u16 a = (c >> 12) & 0xF000;
153 return r | g | b | a;
154}
155
162inline std::string IntToHex(u32 color, bool alpha = true) {
163 std::string hex = "#";
166
167 if (!alpha) hex.resize(7);
168
169 return hex;
170}
171
178inline u32 HexToInt(std::string hex) {
179 NNL_EXPECTS_DBG(!hex.empty() && hex.size() <= 9); // #RRGGBBAA
180 u32 color = 0;
181 if (!hex.empty() && hex.at(0) == '#') hex = hex.substr(1);
182
183 while (hex.length() < 8) hex += "f";
184
185 color = utl::data::SwapEndian((u32)std::strtoul(hex.c_str(), nullptr, 16));
186
187 return color;
188}
189
196inline glm::vec4 IntToFloat(u32 color) noexcept {
197 glm::vec4 fcolor;
198 fcolor.r = (color & 0xFF) / 255.0f;
199 fcolor.g = ((color >> 8) & 0xFF) / 255.0f;
200 fcolor.b = ((color >> 16) & 0xFF) / 255.0f;
201 fcolor.a = ((color >> 24) & 0xFF) / 255.0f;
202
203 return fcolor;
204}
205
213inline u8 FloatToInt(float color) noexcept {
214 color = std::clamp(color, 0.0f, 1.0f);
215 u8 c = static_cast<u8>(std::round(color * 255.0f));
216 return c;
217}
218
226inline u32 FloatToInt(glm::vec3 color) noexcept {
227 color = glm::clamp(color, 0.0f, 1.0f);
228 u32 r = (u32)(color.r * 255.0f);
229 u32 g = ((u32)(color.g * 255.0f)) << 8;
230 u32 b = ((u32)(color.b * 255.0f)) << 16;
231 u32 a = ((u32)(0xFF)) << 24;
232
233 return (a | b | g | r);
234}
235
243inline u32 FloatToInt(glm::vec4 color) {
244 color = glm::clamp(color, 0.0f, 1.0f);
245 u32 r = (u32)(color.r * 255.0f);
246 u32 g = ((u32)(color.g * 255.0f)) << 8;
247 u32 b = ((u32)(color.b * 255.0f)) << 16;
248 u32 a = ((u32)(color.a * 255.0f)) << 24;
249
250 return (a | b | g | r);
251}
252
258inline float SRGBToLinear(float c) noexcept {
259 if (c < 0.04045f)
260 return (c < 0.0f) ? 0.0f : c * (1.0f / 12.92f);
261 else
262 return std::pow((c + 0.055f) * (1.0f / 1.055f), 2.4f);
263}
264
272template <typename T>
274 for (std::size_t i = 0; i < std::min<std::size_t>(color.length(), 3U); i++) {
275 color[i] = SRGBToLinear(color[i]);
276 }
277
278 return color;
279}
280
286inline float LinearToSRGB(float c) noexcept {
287 if (c < 0.0031308f)
288 return (c < 0.0f) ? 0.0f : c * 12.92f;
289 else
290 return 1.055f * std::pow(c, 1.0f / 2.4f) - 0.055f;
291}
292
299template <typename T>
301 for (std::size_t i = 0; i < std::min<std::size_t>(color.length(), 3U); i++) {
302 color[i] = LinearToSRGB(color[i]);
303 }
304
305 return color;
306}
307
308} // namespace utl::color
309} // namespace nnl
Provides functions and classes for handling binary data.
Contains macros and definitions for fixed-width types.
float SRGBToLinear(float c) noexcept
Converts an sRGB color value to a linear color value.
Definition color.hpp:258
u8 Convert4To8(u8 v) noexcept
Converts a 4-bit color channel value to an 8-bit color channel value.
Definition color.hpp:47
glm::vec4 IntToFloat(u32 color) noexcept
Converts an integer color value to a floating-point vector representation.
Definition color.hpp:196
u32 RGBA4444ToRGBA8888(u16 src) noexcept
Converts RGBA4444 color format to RGBA8888 color format.
Definition color.hpp:78
u8 Convert5To8(u8 v) noexcept
Converts a 5-bit color channel value to an 8-bit color channel value.
Definition color.hpp:58
u16 RGBA8888ToRGBA4444(u32 value) noexcept
Converts RGBA8888 color format to RGBA4444 color format.
Definition color.hpp:147
u32 RGBA5551ToRGBA8888(u16 src) noexcept
Converts RGBA5551 color format to RGBA8888 color format.
Definition color.hpp:94
u16 RGBA8888ToRGBA5551(u32 value) noexcept
Converts RGBA8888 color format to RGBA5551 color format.
Definition color.hpp:134
float LinearToSRGB(float c) noexcept
Converts a linear color value to an sRGB color value.
Definition color.hpp:286
u32 HexToInt(std::string hex)
Converts a hex string representation of a color to an integer color value.
Definition color.hpp:178
u32 RGB565ToRGBA8888(u16 src) noexcept
Converts RGB565 color format to RGBA8888 color format.
Definition color.hpp:109
std::string IntToHex(u32 color, bool alpha=true)
Converts an integer color value to a hex string representation.
Definition color.hpp:162
u8 FloatToInt(float color) noexcept
Converts a floating-point color value to an integer color representation.
Definition color.hpp:213
u16 RGBA8888ToRGB565(u32 value) noexcept
Converts RGBA8888 color format to RGB565 color format.
Definition color.hpp:122
u8 Convert6To8(u8 v) noexcept
Converts a 6-bit color channel value to an 8-bit color channel value.
Definition color.hpp:68
#define NNL_EXPECTS_DBG(precondition)
Debug-only precondition check.
Definition contract.hpp:59
T SwapEndian(T src) noexcept
Swaps endianness of a value.
Definition data.hpp:51
std::uint16_t u16
16-bit unsigned integer
Definition fixed_type.hpp:62
std::uint32_t u32
32-bit unsigned integer
Definition fixed_type.hpp:60
std::uint8_t u8
8-bit unsigned integer
Definition fixed_type.hpp:64
std::string IntToHex(T i, bool prefix=false, bool prepend=true)
Converts an integer to a hexadecimal string.
Definition string.hpp:95
Provides functions for color conversion and manipulation.
Definition color.hpp:34
Definition exception.hpp:56
Contains various utility functions for working with strings.