Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
colorspace.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Colorspace *
24 * *
25 * $Archive:: $*
26 * *
27 * Original Author:: Hector Yee *
28 * *
29 * $Author:: $*
30 * *
31 * $Modtime:: $*
32 * *
33 * $Revision:: $*
34 * *
35 *---------------------------------------------------------------------------------------------*
36 * Functions: *
37 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
38
39#if defined(_MSC_VER)
40#pragma once
41#endif
42
43#ifndef COLORSPACE_H
44#define COLORSPACE_H
45
46#include "dx8wrapper.h"
47#include <wwmath.h>
48
49void RGB_To_HSV(Vector3 &hsv,const Vector3 &rgb);
50void HSV_To_RGB(Vector3 &rgb, const Vector3 &hsv);
51void Recolor(Vector3 &rgb, const Vector3 &hsv_shift);
52
53//---------------------------------------------------------------------
54// Color Conversions
55//---------------------------------------------------------------------
56
57inline void RGB_To_HSV(Vector3 &hsv,const Vector3 &rgb)
58// modified from Foley et al. page 592
59// converts rgb[0..1] to h [0,360), s and v in [0,1]
60// negative h values are to signify undefined
61{
62 float max=WWMath::Max(rgb.X,rgb.Y);
63 max=WWMath::Max(max,rgb.Z);
64 float min=WWMath::Min(rgb.X,rgb.Y);
65 min=WWMath::Min(min,rgb.Z);
66
67 // value
68 hsv.Z=max;
69
70 // saturation
71 hsv.Y=(max!=0.0f)?((max-min)/max):0.0f;
72 if (hsv.Y==0.0f) hsv.X=-1.0f;
73 else
74 {
75 float delta=max-min;
76 if (rgb.X==max)
77 hsv.X=(rgb.Y-rgb.Z)/delta;
78 else if (rgb.Y==max)
79 hsv.X=2.0f+ (rgb.Z-rgb.X)/delta;
80 else if (rgb.Z==max)
81 hsv.X=4.0f+ (rgb.X-rgb.Y)/delta;
82 hsv.X*=60.0f;
83 if (hsv.X<0.0f) hsv.X+=360.0f;
84 }
85}
86
87inline void HSV_To_RGB(Vector3 &rgb, const Vector3 &hsv)
88{
89 float h=hsv.X;
90 float s=hsv.Y;
91 float v=hsv.Z;
92
93 if (hsv.Y==0.0f) {
94 rgb.Set(v,v,v);
95 } else {
96 float f,p,q,t;
97
98 int i;
99
100 if (h==360.0f) h=0.0f;
101
102 h/=60.0f;
103 i=WWMath::Floor(h);
104 f=h-i;
105 p=v*(1.0f-s);
106 q=v*(1.0f-(s*f));
107 t=v*(1.0f-(s*(1.0f-f)));
108 switch (i) {
109 case 0:
110 rgb.Set(v,t,p);
111 break;
112 case 1:
113 rgb.Set(q,v,p);
114 break;
115 case 2:
116 rgb.Set(p,v,t);
117 break;
118 case 3:
119 rgb.Set(p,q,v);
120 break;
121 case 4:
122 rgb.Set(t,p,v);
123 break;
124 case 5:
125 rgb.Set(v,p,q);
126 break;
127 }
128 }
129}
130
131inline void Recolor(Vector3 &rgb, const Vector3 &hsv_shift)
132{
133 Vector3 hsv;
134 RGB_To_HSV(hsv,rgb);
135
136 // If the Hue has the "undefined flag" (a negative value), this means that the color is pure
137 // monochrome. In this case do not shift the hue (it is undefined) or the saturation (it is 0
138 // so it cannot be decreased, and increasing it would cause the undefined hue to actually
139 // become visible). In this case, we only modify the value.
140 if (hsv.X<0.0f) hsv+=Vector3(0.0f,0.0f,hsv_shift.Z);
141 else hsv+=hsv_shift;
142
143 // angular mod
144 if (hsv.X<0.0f) hsv.X+=360.0f;
145 if (hsv.X>360.0f) hsv.X-=360.0f;
146 // clamp saturation and value
147 hsv.Y=WWMath::Clamp(hsv.Y,0.0f,1.0f);
148 hsv.Z=WWMath::Clamp(hsv.Z,0.0f,1.0f);
149 HSV_To_RGB(rgb,hsv);
150}
151
152inline void Recolor(unsigned& rgba, const Vector3 &hsv_shift)
153{
154 Vector4 rgba_v = DX8Wrapper::Convert_Color(rgba);
155 Recolor((Vector3&)rgba_v, hsv_shift);
156 rgba = DX8Wrapper::Convert_Color(rgba_v);
157}
158
159
160#endif
161
#define min(x, y)
Definition BaseType.h:101
#define max(x, y)
Definition BaseType.h:105
static Vector4 Convert_Color(unsigned color)
Definition dx8wrapper.h:958
float X
Definition vector3.h:90
float Z
Definition vector3.h:92
float Y
Definition vector3.h:91
WWINLINE void Set(float x, float y, float z)
Definition vector3.h:103
static float Max(float a, float b)
Definition wwmath.h:264
static float Min(float a, float b)
Definition wwmath.h:258
static float Clamp(float val, float min=0.0f, float max=1.0f)
Definition wwmath.h:208
static float Floor(float val)
Definition wwmath.h:153
void Recolor(Vector3 &rgb, const Vector3 &hsv_shift)
Definition colorspace.h:131
void HSV_To_RGB(Vector3 &rgb, const Vector3 &hsv)
Definition colorspace.h:87
void RGB_To_HSV(Vector3 &hsv, const Vector3 &rgb)
Definition colorspace.h:57
int q
Definition test1.cpp:94