// Copyright 2016 The SwiftShader Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef sw_Color_hpp #define sw_Color_hpp #include "Common/Types.hpp" #include "Common/Math.hpp" namespace sw { template struct Color { Color(); Color(const Color &c); Color(const Color &c); Color(const Color &c); Color(int c); Color(unsigned short c); Color(unsigned long c); Color(unsigned int c); Color(T r, T g, T b, T a = 1); operator unsigned int() const; T &operator[](int i); const T &operator[](int i) const; Color operator+() const; Color operator-() const; Color& operator=(const Color& c); Color &operator+=(const Color &c); Color &operator*=(float l); static Color gradient(const Color &c1, const Color &c2, float d); static Color shade(const Color &c1, const Color &c2, float d); template friend Color operator+(const Color &c1, const Color &c2); template friend Color operator-(const Color &c1, const Color &c2); template friend Color operator*(float l, const Color &c); template friend Color operator*(const Color &c1, const Color &c2); template friend Color operator/(const Color &c, float l); T r; T g; T b; T a; }; } #include "Common/Math.hpp" namespace sw { template inline Color::Color() { } template<> inline Color::Color(const Color &c) { r = c.r; g = c.g; b = c.b; a = c.a; } template<> inline Color::Color(const Color &c) { r = clamp(c.r >> 4, 0, 255); g = clamp(c.g >> 4, 0, 255); b = clamp(c.b >> 4, 0, 255); a = clamp(c.a >> 4, 0, 255); } template<> inline Color::Color(const Color &c) { r = ifloor(clamp(c.r * 256.0f, 0.0f, 255.0f)); g = ifloor(clamp(c.g * 256.0f, 0.0f, 255.0f)); b = ifloor(clamp(c.b * 256.0f, 0.0f, 255.0f)); a = ifloor(clamp(c.a * 256.0f, 0.0f, 255.0f)); } template<> inline Color::Color(const Color &c) { r = c.r; g = c.g; b = c.b; a = c.a; } template<> inline Color::Color(const Color &c) { r = c.r << 4; g = c.g << 4; b = c.b << 4; a = c.a << 4; } template<> inline Color::Color(const Color &c) { r = c.r; g = c.g; b = c.b; a = c.a; } template<> inline Color::Color(const Color &c) { r = iround(clamp(c.r * 4095.0f, -4096.0f, 4095.0f)); g = iround(clamp(c.g * 4095.0f, -4096.0f, 4095.0f)); b = iround(clamp(c.b * 4095.0f, -4096.0f, 4095.0f)); a = iround(clamp(c.a * 4095.0f, -4096.0f, 4095.0f)); } template<> inline Color::Color(const Color &c) { r = c.r / 255.0f; g = c.g / 255.0f; b = c.b / 255.0f; a = c.a / 255.0f; } template<> inline Color::Color(const Color &c) { r = c.r / 4095.0f; g = c.g / 4095.0f; b = c.b / 4095.0f; a = c.a / 4095.0f; } template<> inline Color::Color(unsigned short c) { r = (float)(c & 0xF800) / (float)0xF800; g = (float)(c & 0x07E0) / (float)0x07E0; b = (float)(c & 0x001F) / (float)0x001F; a = 1; } template<> inline Color::Color(unsigned short c) { // 4.12 fixed-point format r = ((c & 0xF800) >> 4) + ((c & 0xF800) >> 9) + ((c & 0xF800) >> 14); g = ((c & 0x07E0) << 1) + ((c & 0x07E0) >> 5); b = ((c & 0x001F) << 7) + ((c & 0x001F) << 2) + ((c & 0x001F) >> 3); a = 0x1000; } template<> inline Color::Color(unsigned short c) { r = (byte)(((c & 0xF800) >> 8) + ((c & 0xE000) >> 13)); g = (byte)(((c & 0x07E0) >> 3) + ((c & 0x0600) >> 9)); b = (byte)(((c & 0x001F) << 3) + ((c & 0x001C) >> 2)); a = 0xFF; } template<> inline Color::Color(int c) { const float d = 1.0f / 255.0f; r = (float)((c & 0x00FF0000) >> 16) * d; g = (float)((c & 0x0000FF00) >> 8) * d; b = (float)((c & 0x000000FF) >> 0) * d; a = (float)((c & 0xFF000000) >> 24) * d; } template<> inline Color::Color(int c) { // 4.12 fixed-point format r = (short)((c & 0x00FF0000) >> 12); g = (short)((c & 0x0000FF00) >> 4); b = (short)((c & 0x000000FF) << 4); a = (short)((c & 0xFF000000) >> 20); } template<> inline Color::Color(int c) { r = (byte)((c & 0x00FF0000) >> 16); g = (byte)((c & 0x0000FF00) >> 8); b = (byte)((c & 0x000000FF) >> 0); a = (byte)((c & 0xFF000000) >> 24); } template<> inline Color::Color(unsigned int c) { const float d = 1.0f / 255.0f; r = (float)((c & 0x00FF0000) >> 16) * d; g = (float)((c & 0x0000FF00) >> 8) * d; b = (float)((c & 0x000000FF) >> 0) * d; a = (float)((c & 0xFF000000) >> 24) * d; } template<> inline Color::Color(unsigned int c) { // 4.12 fixed-point format r = (short)((c & 0x00FF0000) >> 12); g = (short)((c & 0x0000FF00) >> 4); b = (short)((c & 0x000000FF) << 4); a = (short)((c & 0xFF000000) >> 20); } template<> inline Color::Color(unsigned int c) { r = (byte)((c & 0x00FF0000) >> 16); g = (byte)((c & 0x0000FF00) >> 8); b = (byte)((c & 0x000000FF) >> 0); a = (byte)((c & 0xFF000000) >> 24); } template<> inline Color::Color(unsigned long c) { const float d = 1.0f / 255.0f; r = (float)((c & 0x00FF0000) >> 16) * d; g = (float)((c & 0x0000FF00) >> 8) * d; b = (float)((c & 0x000000FF) >> 0) * d; a = (float)((c & 0xFF000000) >> 24) * d; } template<> inline Color::Color(unsigned long c) { // 4.12 fixed-point format r = (short)((c & 0x00FF0000) >> 12); g = (short)((c & 0x0000FF00) >> 4); b = (short)((c & 0x000000FF) << 4); a = (short)((c & 0xFF000000) >> 20); } template<> inline Color::Color(unsigned long c) { r = (byte)((c & 0x00FF0000) >> 16); g = (byte)((c & 0x0000FF00) >> 8); b = (byte)((c & 0x000000FF) >> 0); a = (byte)((c & 0xFF000000) >> 24); } template inline Color::Color(T r_, T g_, T b_, T a_) { r = r_; g = g_; b = b_; a = a_; } template<> inline Color::operator unsigned int() const { return ((unsigned int)min(b * 255.0f, 255.0f) << 0) | ((unsigned int)min(g * 255.0f, 255.0f) << 8) | ((unsigned int)min(r * 255.0f, 255.0f) << 16) | ((unsigned int)min(a * 255.0f, 255.0f) << 24); } template<> inline Color::operator unsigned int() const { return ((unsigned int)min(b >> 4, 255) << 0) | ((unsigned int)min(g >> 4, 255) << 8) | ((unsigned int)min(r >> 4, 255) << 16) | ((unsigned int)min(a >> 4, 255) << 24); } template<> inline Color::operator unsigned int() const { return (b << 0) + (g << 8) + (r << 16) + (a << 24); } template inline T &Color::operator[](int i) { return (&r)[i]; } template inline const T &Color::operator[](int i) const { return (&r)[i]; } template inline Color Color::operator+() const { return *this; } template inline Color Color::operator-() const { return Color(-r, -g, -b, -a); } template inline Color &Color::operator=(const Color& c) { r = c.r; g = c.g; b = c.b; a = c.a; return *this; } template inline Color &Color::operator+=(const Color &c) { r += c.r; g += c.g; b += c.b; a += c.a; return *this; } template inline Color &Color::operator*=(float l) { *this = l * *this; return *this; } template inline Color operator+(const Color &c1, const Color &c2) { return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a); } template inline Color operator-(const Color &c1, const Color &c2) { return Color(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a); } template inline Color operator*(float l, const Color &c) { T r = (T)(l * c.r); T g = (T)(l * c.g); T b = (T)(l * c.b); T a = (T)(l * c.a); return Color(r, g, b, a); } template inline Color operator*(const Color &c1, const Color &c2) { T r = c1.r * c2.r; T g = c1.g * c2.g; T b = c1.b * c2.b; T a = c1.a * c2.a; return Color(r, g, b, a); } template<> inline Color operator*(const Color &c1, const Color &c2) { short r = c1.r * c2.r >> 12; short g = c1.g * c2.g >> 12; short b = c1.b * c2.b >> 12; short a = c1.a * c2.a >> 12; return Color(r, g, b, a); } template<> inline Color operator*(const Color &c1, const Color &c2) { byte r = c1.r * c2.r >> 8; byte g = c1.g * c2.g >> 8; byte b = c1.b * c2.b >> 8; byte a = c1.a * c2.a >> 8; return Color(r, g, b, a); } template inline Color operator/(const Color &c, float l) { l = 1.0f / l; T r = (T)(l * c.r); T g = (T)(l * c.g); T b = (T)(l * c.b); T a = (T)(l * c.a); return Color(r, g, b, a); } template inline Color Color::gradient(const Color &c1, const Color &c2, float d) { d = 1.0f / d; T r = (c2.r - c1.r) * d; T g = (c2.g - c1.g) * d; T b = (c2.b - c1.b) * d; T a = (c2.a - c1.a) * d; return Color(r, g, b, a); } template inline Color Color::shade(const Color &c1, const Color &c2, float d) { T r = c1.r + (T)(d * (c2.r - c1.r)); T g = c1.g + (T)(d * (c2.g - c1.g)); T b = c1.b + (T)(d * (c2.b - c1.b)); T a = c1.a + (T)(d * (c2.a - c1.a)); return Color(r, g, b, a); } } #endif // sw_Color_hpp