added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / gfx / color.inl
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This file is part of the graphics core library.
5
6 The graphics core library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 The graphics core library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with the graphics core library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* colors
22  * author: John Tsiombikas 2004
23  */
24
25 inline Color int_color(int r, int g, int b, int a) {
26         return Color((scalar_t)r / 255.0, (scalar_t)g / 255.0, (scalar_t)b / 255.0, (scalar_t)a / 255.0);
27 }
28
29 inline unsigned long pack_color32(const Color &col) {
30         unsigned long r = (unsigned long)(col.r * 255.0);
31         unsigned long g = (unsigned long)(col.g * 255.0);
32         unsigned long b = (unsigned long)(col.b * 255.0);
33         unsigned long a = (unsigned long)(col.a * 255.0);
34         return (a << ALPHA_SHIFT32) | (r << RED_SHIFT32) | (g << GREEN_SHIFT32) | (b << BLUE_SHIFT32);
35 }
36
37 inline unsigned short pack_color16(const Color &col) {
38         unsigned long r = (unsigned long)(col.r * 31.0);
39         unsigned long g = (unsigned long)(col.g * 63.0);
40         unsigned long b = (unsigned long)(col.b * 31.0);
41         return (r << RED_SHIFT16) | (g << GREEN_SHIFT16) | (b << BLUE_SHIFT16);
42 }
43
44 inline unsigned short pack_color15(const Color &col) {
45         unsigned long r = (unsigned long)(col.r * 31.0);
46         unsigned long g = (unsigned long)(col.g * 31.0);
47         unsigned long b = (unsigned long)(col.b * 31.0);
48         return (r << RED_SHIFT15) | (g << GREEN_SHIFT15) | (b << BLUE_SHIFT15);
49 }
50
51 inline Color unpack_color32(unsigned long pcol) {
52         scalar_t a = (scalar_t)((pcol >> ALPHA_SHIFT32) & ALPHA_MASK32) / 255.0;
53         scalar_t r = (scalar_t)((pcol >> RED_SHIFT32) & RED_MASK32) / 255.0;
54         scalar_t g = (scalar_t)((pcol >> GREEN_SHIFT32) & GREEN_MASK32) / 255.0;
55         scalar_t b = (scalar_t)((pcol >> BLUE_SHIFT32) & BLUE_MASK32) / 255.0;
56         return Color(r, g, b, a);
57 }
58
59 inline Color unpack_color16(unsigned short pcol) {
60         scalar_t r = (scalar_t)((pcol >> RED_SHIFT16) & RED_MASK16) / 31.0;
61         scalar_t g = (scalar_t)((pcol >> GREEN_SHIFT16) & GREEN_MASK16) / 63.0;
62         scalar_t b = (scalar_t)((pcol >> BLUE_SHIFT16) & BLUE_MASK16) / 31.0;
63         return Color(r, g, b, 1.0);
64 }
65
66 inline Color unpack_color15(unsigned short pcol) {
67         scalar_t r = (scalar_t)((pcol >> RED_SHIFT15) & RED_MASK15) / 31.0;
68         scalar_t g = (scalar_t)((pcol >> GREEN_SHIFT15) & GREEN_MASK15) / 31.0;
69         scalar_t b = (scalar_t)((pcol >> BLUE_SHIFT15) & BLUE_MASK15) / 31.0;
70         return Color(r, g, b, 1.0);
71 }
72
73 inline Color lookup_color8(int index, const unsigned char **pal) {
74         scalar_t r = (scalar_t)pal[index][0] / 255.0;
75         scalar_t g = (scalar_t)pal[index][1] / 255.0;
76         scalar_t b = (scalar_t)pal[index][2] / 255.0;
77         return Color(r, g, b, 1.0);
78 }
79
80 inline Color blend_colors(const Color &c1, const Color &c2, scalar_t t) {
81         scalar_t r = c1.r + (c2.r - c1.r) * t;
82         scalar_t g = c1.g + (c2.g - c1.g) * t;
83         scalar_t b = c1.b + (c2.b - c1.b) * t;
84         scalar_t a = c1.a + (c2.a - c1.a) * t;
85         return Color(r, g, b, a);
86 }