1// Aseprite Document Library
2// Copyright (c) 2020 Igara Studio S.A.
3// Copyright (c) 2001-2016 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef DOC_COLOR_H_INCLUDED
9#define DOC_COLOR_H_INCLUDED
10#pragma once
11
12#include "base/ints.h"
13
14namespace doc {
15
16 // The greatest int type to storage a color for an image in the
17 // available pixel formats.
18 typedef uint32_t color_t;
19
20 //////////////////////////////////////////////////////////////////////
21 // RGBA
22
23 const uint32_t rgba_r_shift = 0;
24 const uint32_t rgba_g_shift = 8;
25 const uint32_t rgba_b_shift = 16;
26 const uint32_t rgba_a_shift = 24;
27
28 const uint32_t rgba_r_mask = 0x000000ff;
29 const uint32_t rgba_g_mask = 0x0000ff00;
30 const uint32_t rgba_b_mask = 0x00ff0000;
31 const uint32_t rgba_rgb_mask = 0x00ffffff;
32 const uint32_t rgba_a_mask = 0xff000000;
33
34 inline uint8_t rgba_getr(uint32_t c) {
35 return (c >> rgba_r_shift) & 0xff;
36 }
37
38 inline uint8_t rgba_getg(uint32_t c) {
39 return (c >> rgba_g_shift) & 0xff;
40 }
41
42 inline uint8_t rgba_getb(uint32_t c) {
43 return (c >> rgba_b_shift) & 0xff;
44 }
45
46 inline uint8_t rgba_geta(uint32_t c) {
47 return (c >> rgba_a_shift) & 0xff;
48 }
49
50 inline uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
51 return ((r << rgba_r_shift) |
52 (g << rgba_g_shift) |
53 (b << rgba_b_shift) |
54 (a << rgba_a_shift));
55 }
56
57 inline int rgb_luma(int r, int g, int b) {
58 return (r*2126 + g*7152 + b*722) / 10000;
59 }
60
61 inline uint8_t rgba_luma(uint32_t c) {
62 return rgb_luma(rgba_getr(c), rgba_getg(c), rgba_getb(c));
63 }
64
65 //////////////////////////////////////////////////////////////////////
66 // Grayscale
67
68 const uint16_t graya_v_shift = 0;
69 const uint16_t graya_a_shift = 8;
70
71 const uint16_t graya_v_mask = 0x00ff;
72 const uint16_t graya_a_mask = 0xff00;
73
74 inline uint8_t graya_getv(uint16_t c) {
75 return (c >> graya_v_shift) & 0xff;
76 }
77
78 inline uint8_t graya_geta(uint16_t c) {
79 return (c >> graya_a_shift) & 0xff;
80 }
81
82 inline uint16_t graya(uint8_t v, uint8_t a) {
83 return ((v << graya_v_shift) |
84 (a << graya_a_shift));
85 }
86
87 inline uint16_t gray(uint8_t v) {
88 return graya(v, 255);
89 }
90
91 //////////////////////////////////////////////////////////////////////
92 // Conversions
93
94 typedef color_t (*rgba_to_graya_func)(const color_t c);
95
96 color_t rgba_to_graya_using_hsv(const color_t c);
97 color_t rgba_to_graya_using_hsl(const color_t c);
98 color_t rgba_to_graya_using_luma(const color_t c);
99
100} // namespace doc
101
102#endif
103