Blender V2.61 - r43446

node_color.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or
00003  * modify it under the terms of the GNU General Public License
00004  * as published by the Free Software Foundation; either version 2
00005  * of the License, or (at your option) any later version.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  *
00012  * You should have received a copy of the GNU General Public License
00013  * along with this program; if not, write to the Free Software Foundation,
00014  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00015  */
00016 
00017 /* Color Management */
00018 
00019 float color_srgb_to_scene_linear(float c)
00020 {
00021     if(c < 0.04045)
00022         return (c < 0.0)? 0.0: c * (1.0/12.92);
00023     else
00024         return pow((c + 0.055)*(1.0/1.055), 2.4);
00025 }
00026 
00027 float color_scene_linear_to_srgb(float c)
00028 {
00029     if(c < 0.0031308)
00030         return (c < 0.0)? 0.0: c * 12.92;
00031     else
00032         return 1.055 * pow(c, 1.0/2.4) - 0.055;
00033 }
00034 
00035 color color_srgb_to_scene_linear(color c)
00036 {
00037     return color(
00038         color_srgb_to_scene_linear(c[0]),
00039         color_srgb_to_scene_linear(c[1]),
00040         color_srgb_to_scene_linear(c[2]));
00041 }
00042 
00043 color color_scene_linear_to_srgb(color c)
00044 {
00045     return color(
00046         color_scene_linear_to_srgb(c[0]),
00047         color_scene_linear_to_srgb(c[1]),
00048         color_scene_linear_to_srgb(c[2]));
00049 }
00050 
00051 /* Color Operations */
00052 
00053 color rgb_to_hsv(color rgb)
00054 {
00055     float cmax, cmin, h, s, v, cdelta;
00056     color c;
00057 
00058     cmax = max(rgb[0], max(rgb[1], rgb[2]));
00059     cmin = min(rgb[0], min(rgb[1], rgb[2]));
00060     cdelta = cmax - cmin;
00061 
00062     v = cmax;
00063 
00064     if(cmax != 0.0) {
00065         s = cdelta/cmax;
00066     }
00067     else {
00068         s = 0.0;
00069         h = 0.0;
00070     }
00071 
00072     if(s == 0.0) {
00073         h = 0.0;
00074     }
00075     else {
00076         c = (color(cmax, cmax, cmax) - rgb)/cdelta;
00077 
00078         if(rgb[0] == cmax) h = c[2] - c[1];
00079         else if(rgb[1] == cmax) h = 2.0 + c[0] -  c[2];
00080         else h = 4.0 + c[1] - c[0];
00081 
00082         h /= 6.0;
00083 
00084         if(h < 0.0)
00085             h += 1.0;
00086     }
00087 
00088     return color(h, s, v);
00089 }
00090 
00091 color hsv_to_rgb(color hsv)
00092 {
00093     float i, f, p, q, t, h, s, v;
00094     color rgb;
00095 
00096     h = hsv[0];
00097     s = hsv[1];
00098     v = hsv[2];
00099 
00100     if(s==0.0) {
00101         rgb = color(v, v, v);
00102     }
00103     else {
00104         if(h==1.0)
00105             h = 0.0;
00106         
00107         h *= 6.0;
00108         i = floor(h);
00109         f = h - i;
00110         rgb = color(f, f, f);
00111         p = v*(1.0-s);
00112         q = v*(1.0-(s*f));
00113         t = v*(1.0-(s*(1.0-f)));
00114         
00115         if(i == 0.0) rgb = color(v, t, p);
00116         else if(i == 1.0) rgb = color(q, v, p);
00117         else if(i == 2.0) rgb = color(p, v, t);
00118         else if(i == 3.0) rgb = color(p, q, v);
00119         else if(i == 4.0) rgb = color(t, p, v);
00120         else rgb = color(v, p, q);
00121     }
00122 
00123     return rgb;
00124 }
00125