// // SPDX-License-Identifier: BSD-3-Clause // Copyright Contributors to the OpenEXR Project. // // // Color conversion functions and general color algorithms // #ifndef INCLUDED_IMATHCOLORALGO_H #define INCLUDED_IMATHCOLORALGO_H #include "ImathNamespace.h" #include "ImathExport.h" #include "ImathColor.h" #include "ImathMath.h" IMATH_INTERNAL_NAMESPACE_HEADER_ENTER // // Non-templated helper routines for color conversion. // These routines eliminate type warnings under g++. // /// /// Convert 3-channel hsv to rgb. Non-templated helper routine. IMATH_EXPORT Vec3 hsv2rgb_d (const Vec3& hsv) IMATH_NOEXCEPT; /// /// Convert 4-channel hsv to rgb (with alpha). Non-templated helper routine. IMATH_EXPORT Color4 hsv2rgb_d (const Color4& hsv) IMATH_NOEXCEPT; /// /// Convert 3-channel rgb to hsv. Non-templated helper routine. IMATH_EXPORT Vec3 rgb2hsv_d (const Vec3& rgb) IMATH_NOEXCEPT; /// /// Convert 4-channel rgb to hsv. Non-templated helper routine. IMATH_EXPORT Color4 rgb2hsv_d (const Color4& rgb) IMATH_NOEXCEPT; /// /// Convert 3-channel hsv to rgb. /// template IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3 hsv2rgb (const Vec3& hsv) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { Vec3 v = Vec3 (hsv.x / double (std::numeric_limits::max()), hsv.y / double (std::numeric_limits::max()), hsv.z / double (std::numeric_limits::max())); Vec3 c = hsv2rgb_d (v); return Vec3 ((T) (c.x * std::numeric_limits::max()), (T) (c.y * std::numeric_limits::max()), (T) (c.z * std::numeric_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); } } /// /// Convert 4-channel hsv to rgb (with alpha). /// template IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Color4 hsv2rgb (const Color4& hsv) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { Color4 v = Color4 (hsv.r / float (std::numeric_limits::max()), hsv.g / float (std::numeric_limits::max()), hsv.b / float (std::numeric_limits::max()), hsv.a / float (std::numeric_limits::max())); Color4 c = hsv2rgb_d (v); return Color4 ((T) (c.r * std::numeric_limits::max()), (T) (c.g * std::numeric_limits::max()), (T) (c.b * std::numeric_limits::max()), (T) (c.a * std::numeric_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); } } /// /// Convert 3-channel rgb to hsv. /// template IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3 rgb2hsv (const Vec3& rgb) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { Vec3 v = Vec3 (rgb.x / double (std::numeric_limits::max()), rgb.y / double (std::numeric_limits::max()), rgb.z / double (std::numeric_limits::max())); Vec3 c = rgb2hsv_d (v); return Vec3 ((T) (c.x * std::numeric_limits::max()), (T) (c.y * std::numeric_limits::max()), (T) (c.z * std::numeric_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); } } /// /// Convert 4-channel rgb to hsv (with alpha). /// template IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Color4 rgb2hsv (const Color4& rgb) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { Color4 v = Color4 (rgb.r / float (std::numeric_limits::max()), rgb.g / float (std::numeric_limits::max()), rgb.b / float (std::numeric_limits::max()), rgb.a / float (std::numeric_limits::max())); Color4 c = rgb2hsv_d (v); return Color4 ((T) (c.r * std::numeric_limits::max()), (T) (c.g * std::numeric_limits::max()), (T) (c.b * std::numeric_limits::max()), (T) (c.a * std::numeric_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); } } /// /// Convert 3-channel rgb to PackedColor /// template IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor rgb2packed (const Vec3& c) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { float x = c.x / float (std::numeric_limits::max()); float y = c.y / float (std::numeric_limits::max()); float z = c.z / float (std::numeric_limits::max()); return rgb2packed (V3f (x, y, z)); } else { // clang-format off return ( (PackedColor) (c.x * 255) | (((PackedColor) (c.y * 255)) << 8) | (((PackedColor) (c.z * 255)) << 16) | 0xFF000000 ); // clang-format on } } /// /// Convert 4-channel rgb to PackedColor (with alpha) /// template IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor rgb2packed (const Color4& c) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { float r = c.r / float (std::numeric_limits::max()); float g = c.g / float (std::numeric_limits::max()); float b = c.b / float (std::numeric_limits::max()); float a = c.a / float (std::numeric_limits::max()); return rgb2packed (C4f (r, g, b, a)); } else { // clang-format off return ( (PackedColor) (c.r * 255) | (((PackedColor) (c.g * 255)) << 8) | (((PackedColor) (c.b * 255)) << 16) | (((PackedColor) (c.a * 255)) << 24)); // clang-format on } } /// /// Convert PackedColor to 3-channel rgb. Return the result in the /// `out` parameter. /// template IMATH_HOSTDEVICE void packed2rgb (PackedColor packed, Vec3& out) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { T f = std::numeric_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; } } /// /// Convert PackedColor to 4-channel rgb (with alpha). Return the /// result in the `out` parameter. /// template IMATH_HOSTDEVICE void packed2rgb (PackedColor packed, Color4& out) IMATH_NOEXCEPT { if (std::numeric_limits::is_integer) { T f = std::numeric_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