CColor clamp for all arithmetic ops

This commit is contained in:
Jack Andersen 2019-03-23 22:06:25 -10:00
parent cb4ede8097
commit 2edc75f793
2 changed files with 24 additions and 17 deletions

View File

@ -133,59 +133,67 @@ public:
bool operator!=(const CColor& rhs) const { return !(*this == rhs); } bool operator!=(const CColor& rhs) const { return !(*this == rhs); }
CColor operator+(const CColor& rhs) const { return mSimd + rhs.mSimd; } CColor operator+(const CColor& rhs) const { return CColor(mSimd + rhs.mSimd).Clamp(); }
CColor operator-(const CColor& rhs) const { return mSimd - rhs.mSimd; } CColor operator-(const CColor& rhs) const { return CColor(mSimd - rhs.mSimd).Clamp(); }
CColor operator*(const CColor& rhs) const { return mSimd * rhs.mSimd; } CColor operator*(const CColor& rhs) const { return CColor(mSimd * rhs.mSimd).Clamp(); }
CColor operator/(const CColor& rhs) const { return mSimd / rhs.mSimd; } CColor operator/(const CColor& rhs) const { return CColor(mSimd / rhs.mSimd).Clamp(); }
CColor operator+(float val) const { return mSimd + simd<float>(val); } CColor operator+(float val) const { return CColor(mSimd + simd<float>(val)).Clamp(); }
CColor operator-(float val) const { return mSimd - simd<float>(val); } CColor operator-(float val) const { return CColor(mSimd - simd<float>(val)).Clamp(); }
CColor operator*(float val) const { return mSimd * simd<float>(val); } CColor operator*(float val) const { return CColor(mSimd * simd<float>(val)).Clamp(); }
CColor operator/(float val) const { return mSimd / simd<float>(val); } CColor operator/(float val) const { return CColor(mSimd / simd<float>(val)).Clamp(); }
const CColor& operator+=(const CColor& rhs) { const CColor& operator+=(const CColor& rhs) {
mSimd += rhs.mSimd; mSimd += rhs.mSimd;
Clamp();
return *this; return *this;
} }
const CColor& operator-=(const CColor& rhs) { const CColor& operator-=(const CColor& rhs) {
mSimd -= rhs.mSimd; mSimd -= rhs.mSimd;
Clamp();
return *this; return *this;
} }
const CColor& operator*=(const CColor& rhs) { const CColor& operator*=(const CColor& rhs) {
mSimd *= rhs.mSimd; mSimd *= rhs.mSimd;
Clamp();
return *this; return *this;
} }
const CColor& operator/=(const CColor& rhs) { const CColor& operator/=(const CColor& rhs) {
mSimd /= rhs.mSimd; mSimd /= rhs.mSimd;
Clamp();
return *this; return *this;
} }
const CColor& operator+=(float rhs) { const CColor& operator+=(float rhs) {
mSimd += simd<float>(rhs); mSimd += simd<float>(rhs);
Clamp();
return *this; return *this;
} }
const CColor& operator-=(float rhs) { const CColor& operator-=(float rhs) {
mSimd -= simd<float>(rhs); mSimd -= simd<float>(rhs);
Clamp();
return *this; return *this;
} }
const CColor& operator*=(float rhs) { const CColor& operator*=(float rhs) {
mSimd *= simd<float>(rhs); mSimd *= simd<float>(rhs);
Clamp();
return *this; return *this;
} }
const CColor& operator/=(float rhs) { const CColor& operator/=(float rhs) {
mSimd /= simd<float>(rhs); mSimd /= simd<float>(rhs);
Clamp();
return *this; return *this;
} }
@ -279,11 +287,12 @@ public:
/** /**
* @brief Clamps to GPU-safe RGBA values [0,1] * @brief Clamps to GPU-safe RGBA values [0,1]
*/ */
void Clamp() { CColor& Clamp() {
r() = std::min(1.f, std::max(0.f, float(r()))); r() = std::min(1.f, std::max(0.f, float(r())));
g() = std::min(1.f, std::max(0.f, float(g()))); g() = std::min(1.f, std::max(0.f, float(g())));
b() = std::min(1.f, std::max(0.f, float(b()))); b() = std::min(1.f, std::max(0.f, float(b())));
a() = std::min(1.f, std::max(0.f, float(a()))); a() = std::min(1.f, std::max(0.f, float(a())));
return *this;
} }
float r() const { return mSimd[0]; } float r() const { return mSimd[0]; }
@ -314,11 +323,11 @@ constexpr CColor skYellow(1.f, 1.f, 0.f, 1.f);
constexpr CColor skWhite(1.f, 1.f, 1.f, 1.f); constexpr CColor skWhite(1.f, 1.f, 1.f, 1.f);
constexpr CColor skClear(0.f, 0.f, 0.f, 0.f); constexpr CColor skClear(0.f, 0.f, 0.f, 0.f);
inline CColor operator+(float lhs, const CColor& rhs) { return simd<float>(lhs) + rhs.mSimd; } inline CColor operator+(float lhs, const CColor& rhs) { return CColor(simd<float>(lhs) + rhs.mSimd).Clamp(); }
inline CColor operator-(float lhs, const CColor& rhs) { return simd<float>(lhs) - rhs.mSimd; } inline CColor operator-(float lhs, const CColor& rhs) { return CColor(simd<float>(lhs) - rhs.mSimd).Clamp(); }
inline CColor operator*(float lhs, const CColor& rhs) { return simd<float>(lhs) * rhs.mSimd; } inline CColor operator*(float lhs, const CColor& rhs) { return CColor(simd<float>(lhs) * rhs.mSimd).Clamp(); }
inline CColor operator/(float lhs, const CColor& rhs) { return simd<float>(lhs) / rhs.mSimd; } inline CColor operator/(float lhs, const CColor& rhs) { return CColor(simd<float>(lhs) / rhs.mSimd).Clamp(); }
} // namespace zeus } // namespace zeus

View File

@ -157,10 +157,8 @@ public:
simd<float>{0.f, 0.f, factor, 0.f})); simd<float>{0.f, 0.f, factor, 0.f}));
} }
CTransform multiplyIgnoreTranslation(const CTransform& xfrm) const { CTransform multiplyIgnoreTranslation(const CTransform& rhs) const {
CTransform ret; return CTransform(basis * rhs.basis, origin + rhs.origin);
ret.basis = basis * xfrm.basis;
return ret;
} }
CTransform getRotation() const { CTransform getRotation() const {