summaryrefslogtreecommitdiffstats
path: root/src/graphics/common/color.cpp
blob: bc5cf5d06d0d3f838f6c38df1148480e72f58f68 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// * This file is part of the COLOBOT source code
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

// color.cpp

#include "graphics/common/color.h"

// TODO


// Returns the color corresponding D3DCOLOR.

D3DCOLOR RetColor(float intensity)
{
  D3DCOLOR  color;
  
  if ( intensity <= 0.0f )  return 0x00000000;
  if ( intensity >= 1.0f )  return 0xffffffff;
  
  color  = (int)(intensity*255.0f)<<24;
  color |= (int)(intensity*255.0f)<<16;
  color |= (int)(intensity*255.0f)<<8;
  color |= (int)(intensity*255.0f);
  
  return color;
}

// Returns the color corresponding D3DCOLOR.

D3DCOLOR RetColor(D3DCOLORVALUE intensity)
{
  D3DCOLOR  color;
  
  color  = (int)(intensity.a*255.0f)<<24;
  color |= (int)(intensity.r*255.0f)<<16;
  color |= (int)(intensity.g*255.0f)<<8;
  color |= (int)(intensity.b*255.0f);
  
  return color;
}

// Returns the color corresponding D3DCOLORVALUE.

D3DCOLORVALUE RetColor(D3DCOLOR intensity)
{
  D3DCOLORVALUE color;
  
  color.r = (float)((intensity>>16)&0xff)/256.0f;
  color.g = (float)((intensity>>8 )&0xff)/256.0f;
  color.b = (float)((intensity>>0 )&0xff)/256.0f;
  color.a = (float)((intensity>>24)&0xff)/256.0f;
  
  return color;
}


// RGB to HSV conversion.

void RGB2HSV(D3DCOLORVALUE src, ColorHSV &dest)
{
  float min, max, delta;
  
  min = Min(src.r, src.g, src.b);
  max = Max(src.r, src.g, src.b);
  
  dest.v = max;  // intensity
  
  if ( max == 0.0f )
  {
    dest.s = 0.0f;  // saturation
    dest.h = 0.0f;  // undefined color!
  }
  else
  {
    delta = max-min;
    dest.s = delta/max;  // saturation
    
    if ( src.r == max )  // between yellow & magenta
    {
      dest.h = (src.g-src.b)/delta;
    }
    else if ( src.g == max )  // between cyan & yellow
    {
      dest.h = 2.0f+(src.b-src.r)/delta;
    }
    else  // between magenta & cyan
    {
      dest.h = 4.0f+(src.r-src.g)/delta;
    }
    
    dest.h *= 60.0f;  // in degrees
    if ( dest.h < 0.0f )  dest.h += 360.0f;
    dest.h /= 360.0f;  // 0..1
  }
}

// HSV to RGB conversion.

void HSV2RGB(ColorHSV src, D3DCOLORVALUE &dest)
{
  int   i;
  float f,v,p,q,t;
  
  src.h = Norm(src.h)*360.0f;
  src.s = Norm(src.s);
  src.v = Norm(src.v);
  
  if ( src.s == 0.0f )  // zero saturation?
  {
    dest.r = src.v;
    dest.g = src.v;
    dest.b = src.v;  // gray
  }
  else
  {
    if ( src.h == 360.0f )  src.h = 0.0f;
    src.h /= 60.0f;
    i = (int)src.h;  // integer part (0 .. 5)
    f = src.h-i;     // fractional part
    
    v = src.v;
    p = src.v*(1.0f-src.s);
    q = src.v*(1.0f-(src.s*f));
    t = src.v*(1.0f-(src.s*(1.0f-f)));
    
    switch (i)
    {
      case 0:  dest.r=v; dest.g=t; dest.b=p;  break;
      case 1:  dest.r=q; dest.g=v; dest.b=p;  break;
      case 2:  dest.r=p; dest.g=v; dest.b=t;  break;
      case 3:  dest.r=p; dest.g=q; dest.b=v;  break;
      case 4:  dest.r=t; dest.g=p; dest.b=v;  break;
      case 5:  dest.r=v; dest.g=p; dest.b=q;  break;
    }
  }
}