/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Industrial Light & Magic nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// #ifndef INCLUDED_IMATHCOLORALGO_H #define INCLUDED_IMATHCOLORALGO_H #include "ImathColor.h" #include "ImathExport.h" #include "ImathMath.h" #include "ImathLimits.h" #include "ImathNamespace.h" IMATH_INTERNAL_NAMESPACE_HEADER_ENTER // // Non-templated helper routines for color conversion. // These routines eliminate type warnings under g++. // IMATH_EXPORT Vec3 hsv2rgb_d(const Vec3 &hsv); IMATH_EXPORT Color4 hsv2rgb_d(const Color4 &hsv); IMATH_EXPORT Vec3 rgb2hsv_d(const Vec3 &rgb); IMATH_EXPORT Color4 rgb2hsv_d(const Color4 &rgb); // // Color conversion functions and general color algorithms // // hsv2rgb(), rgb2hsv(), rgb2packed(), packed2rgb() // see each funtion definition for details. // template Vec3 hsv2rgb(const Vec3 &hsv) { if ( limits::isIntegral() ) { Vec3 v = Vec3(hsv.x / double(limits::max()), hsv.y / double(limits::max()), hsv.z / double(limits::max())); Vec3 c = hsv2rgb_d(v); return Vec3((T) (c.x * limits::max()), (T) (c.y * limits::max()), (T) (c.z * limits::max())); } else { Vec3 v = Vec3(hsv.x, hsv.y, hsv.z); Vec3 c = hsv2rgb_d(v); return Vec3((T) c.x, (T) c.y, (T) c.z); } } template Color4 hsv2rgb(const Color4 &hsv) { if ( limits::isIntegral() ) { Color4 v = Color4(hsv.r / float(limits::max()), hsv.g / float(limits::max()), hsv.b / float(limits::max()), hsv.a / float(limits::max())); Color4 c = hsv2rgb_d(v); return Color4((T) (c.r * limits::max()), (T) (c.g * limits::max()), (T) (c.b * limits::max()), (T) (c.a * limits::max())); } else { Color4 v = Color4(hsv.r, hsv.g, hsv.b, hsv.a); Color4 c = hsv2rgb_d(v); return Color4((T) c.r, (T) c.g, (T) c.b, (T) c.a); } } template Vec3 rgb2hsv(const Vec3 &rgb) { if ( limits::isIntegral() ) { Vec3 v = Vec3(rgb.x / double(limits::max()), rgb.y / double(limits::max()), rgb.z / double(limits::max())); Vec3 c = rgb2hsv_d(v); return Vec3((T) (c.x * limits::max()), (T) (c.y * limits::max()), (T) (c.z * limits::max())); } else { Vec3 v = Vec3(rgb.x, rgb.y, rgb.z); Vec3 c = rgb2hsv_d(v); return Vec3((T) c.x, (T) c.y, (T) c.z); } } template Color4 rgb2hsv(const Color4 &rgb) { if ( limits::isIntegral() ) { Color4 v = Color4(rgb.r / float(limits::max()), rgb.g / float(limits::max()), rgb.b / float(limits::max()), rgb.a / float(limits::max())); Color4 c = rgb2hsv_d(v); return Color4((T) (c.r * limits::max()), (T) (c.g * limits::max()), (T) (c.b * limits::max()), (T) (c.a * limits::max())); } else { Color4 v = Color4(rgb.r, rgb.g, rgb.b, rgb.a); Color4 c = rgb2hsv_d(v); return Color4((T) c.r, (T) c.g, (T) c.b, (T) c.a); } } template PackedColor rgb2packed(const Vec3 &c) { if ( limits::isIntegral() ) { float x = c.x / float(limits::max()); float y = c.y / float(limits::max()); float z = c.z / float(limits::max()); return rgb2packed( V3f(x,y,z) ); } else { return ( (PackedColor) (c.x * 255) | (((PackedColor) (c.y * 255)) << 8) | (((PackedColor) (c.z * 255)) << 16) | 0xFF000000 ); } } template PackedColor rgb2packed(const Color4 &c) { if ( limits::isIntegral() ) { float r = c.r / float(limits::max()); float g = c.g / float(limits::max()); float b = c.b / float(limits::max()); float a = c.a / float(limits::max()); return rgb2packed( C4f(r,g,b,a) ); } else { return ( (PackedColor) (c.r * 255) | (((PackedColor) (c.g * 255)) << 8) | (((PackedColor) (c.b * 255)) << 16) | (((PackedColor) (c.a * 255)) << 24)); } } // // This guy can't return the result because the template // parameter would not be in the function signiture. So instead, // its passed in as an argument. // template void packed2rgb(PackedColor packed, Vec3 &out) { if ( limits::isIntegral() ) { T f = limits::max() / ((PackedColor)0xFF); out.x = (packed & 0xFF) * f; out.y = ((packed & 0xFF00) >> 8) * f; out.z = ((packed & 0xFF0000) >> 16) * f; } else { T f = T(1) / T(255); out.x = (packed & 0xFF) * f; out.y = ((packed & 0xFF00) >> 8) * f; out.z = ((packed & 0xFF0000) >> 16) * f; } } template void packed2rgb(PackedColor packed, Color4 &out) { if ( limits::isIntegral() ) { T f = limits::max() / ((PackedColor)0xFF); out.r = (packed & 0xFF) * f; out.g = ((packed & 0xFF00) >> 8) * f; out.b = ((packed & 0xFF0000) >> 16) * f; out.a = ((packed & 0xFF000000) >> 24) * f; } else { T f = T(1) / T(255); out.r = (packed & 0xFF) * f; out.g = ((packed & 0xFF00) >> 8) * f; out.b = ((packed & 0xFF0000) >> 16) * f; out.a = ((packed & 0xFF000000) >> 24) * f; } } IMATH_INTERNAL_NAMESPACE_HEADER_EXIT #endif // INCLUDED_IMATHCOLORALGO_H