Path: blob/master/3rdparty/openexr/Imath/ImathColorAlgo.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////333435//----------------------------------------------------------------------------36//37// Implementation of non-template items declared in ImathColorAlgo.h38//39//----------------------------------------------------------------------------4041#include "ImathColorAlgo.h"4243namespace Imath {444546Vec3<double>47hsv2rgb_d(const Vec3<double> &hsv)48{49double hue = hsv.x;50double sat = hsv.y;51double val = hsv.z;5253double x = 0.0, y = 0.0, z = 0.0;5455if (hue == 1) hue = 0;56else hue *= 6;5758int i = int(Math<double>::floor(hue));59double f = hue-i;60double p = val*(1-sat);61double q = val*(1-(sat*f));62double t = val*(1-(sat*(1-f)));6364switch (i)65{66case 0: x = val; y = t; z = p; break;67case 1: x = q; y = val; z = p; break;68case 2: x = p; y = val; z = t; break;69case 3: x = p; y = q; z = val; break;70case 4: x = t; y = p; z = val; break;71case 5: x = val; y = p; z = q; break;72}7374return Vec3<double>(x,y,z);75}767778Color4<double>79hsv2rgb_d(const Color4<double> &hsv)80{81double hue = hsv.r;82double sat = hsv.g;83double val = hsv.b;8485double r = 0.0, g = 0.0, b = 0.0;8687if (hue == 1) hue = 0;88else hue *= 6;8990int i = int(Math<double>::floor(hue));91double f = hue-i;92double p = val*(1-sat);93double q = val*(1-(sat*f));94double t = val*(1-(sat*(1-f)));9596switch (i)97{98case 0: r = val; g = t; b = p; break;99case 1: r = q; g = val; b = p; break;100case 2: r = p; g = val; b = t; break;101case 3: r = p; g = q; b = val; break;102case 4: r = t; g = p; b = val; break;103case 5: r = val; g = p; b = q; break;104}105106return Color4<double>(r,g,b,hsv.a);107}108109110111Vec3<double>112rgb2hsv_d(const Vec3<double> &c)113{114const double &x = c.x;115const double &y = c.y;116const double &z = c.z;117118double max = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);119double min = (x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z);120double range = max - min;121double val = max;122double sat = 0;123double hue = 0;124125if (max != 0) sat = range/max;126127if (sat != 0)128{129double h;130131if (x == max) h = (y - z) / range;132else if (y == max) h = 2 + (z - x) / range;133else h = 4 + (x - y) / range;134135hue = h/6.;136137if (hue < 0.)138hue += 1.0;139}140return Vec3<double>(hue,sat,val);141}142143144Color4<double>145rgb2hsv_d(const Color4<double> &c)146{147const double &r = c.r;148const double &g = c.g;149const double &b = c.b;150151double max = (r > g) ? ((r > b) ? r : b) : ((g > b) ? g : b);152double min = (r < g) ? ((r < b) ? r : b) : ((g < b) ? g : b);153double range = max - min;154double val = max;155double sat = 0;156double hue = 0;157158if (max != 0) sat = range/max;159160if (sat != 0)161{162double h;163164if (r == max) h = (g - b) / range;165else if (g == max) h = 2 + (b - r) / range;166else h = 4 + (r - g) / range;167168hue = h/6.;169170if (hue < 0.)171hue += 1.0;172}173return Color4<double>(hue,sat,val,c.a);174}175176177} // namespace Imath178179180