diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-09-17 12:43:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-17 12:43:21 +0200 |
commit | 2db9973ad4f363cac9848ffa65f6a78b6c2814b4 (patch) | |
tree | 5576f58ad82bd403a37583b40aa4ff66869fddd2 | |
parent | a29559bc0e8bfbfcab7e0f4b05b9e77119d71ce0 (diff) | |
parent | 9d9cfc6f61a8bad7714f576ea9eb95a18d18b4f6 (diff) |
Merge pull request #11176 from bncastle/master
Implement +,-,/, * and negate operators for Color type
-rw-r--r-- | core/color.cpp | 119 | ||||
-rw-r--r-- | core/color.h | 17 | ||||
-rw-r--r-- | core/variant_op.cpp | 10 |
3 files changed, 141 insertions, 5 deletions
diff --git a/core/color.cpp b/core/color.cpp index 259a4988b1..dd8b13c047 100644 --- a/core/color.cpp +++ b/core/color.cpp @@ -400,3 +400,122 @@ Color::operator String() const { return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a); } + +Color Color::operator+(const Color &p_color) const { + + return Color( + CLAMP(r + p_color.r, 0.0, 1.0), + CLAMP(g + p_color.g, 0.0, 1.0), + CLAMP(b + p_color.b, 0.0, 1.0), + CLAMP(a + p_color.a, 0.0, 1.0)); +} + +void Color::operator+=(const Color &p_color) { + + r = CLAMP(r + p_color.r, 0.0, 1.0); + g = CLAMP(g + p_color.g, 0.0, 1.0); + b = CLAMP(b + p_color.b, 0.0, 1.0); + a = CLAMP(a + p_color.a, 0.0, 1.0); +} + +Color Color::operator-(const Color &p_color) const { + + return Color( + CLAMP(r - p_color.r, 0.0, 1.0), + CLAMP(g - p_color.g, 0.0, 1.0), + CLAMP(b - p_color.b, 0.0, 1.0), + CLAMP(a - p_color.a, 0.0, 1.0)); +} + +void Color::operator-=(const Color &p_color) { + + r = CLAMP(r - p_color.r, 0.0, 1.0); + g = CLAMP(g - p_color.g, 0.0, 1.0); + b = CLAMP(b - p_color.b, 0.0, 1.0); + a = CLAMP(a - p_color.a, 0.0, 1.0); +} + +Color Color::operator*(const Color &p_color) const { + + return Color( + CLAMP(r * p_color.r, 0.0, 1.0), + CLAMP(g * p_color.g, 0.0, 1.0), + CLAMP(b * p_color.b, 0.0, 1.0), + CLAMP(a * p_color.a, 0.0, 1.0)); +} + +Color Color::operator*(const real_t &rvalue) const { + + return Color( + CLAMP(r * rvalue, 0.0, 1.0), + CLAMP(g * rvalue, 0.0, 1.0), + CLAMP(b * rvalue, 0.0, 1.0), + CLAMP(a * rvalue, 0.0, 1.0)); +} + +void Color::operator*=(const Color &p_color) { + + r = CLAMP(r * p_color.r, 0.0, 1.0); + g = CLAMP(g * p_color.g, 0.0, 1.0); + b = CLAMP(b * p_color.b, 0.0, 1.0); + a = CLAMP(a * p_color.a, 0.0, 1.0); +} + +void Color::operator*=(const real_t &rvalue) { + + r = CLAMP(r * rvalue, 0.0, 1.0); + g = CLAMP(g * rvalue, 0.0, 1.0); + b = CLAMP(b * rvalue, 0.0, 1.0); + a = CLAMP(a * rvalue, 0.0, 1.0); +}; + +Color Color::operator/(const Color &p_color) const { + + return Color( + p_color.r == 0 ? 1 : CLAMP(r / p_color.r, 0.0, 1.0), + p_color.g == 0 ? 1 : CLAMP(g / p_color.g, 0.0, 1.0), + p_color.b == 0 ? 1 : CLAMP(b / p_color.b, 0.0, 1.0), + p_color.a == 0 ? 1 : CLAMP(a / p_color.a, 0.0, 1.0)); +} + +Color Color::operator/(const real_t &rvalue) const { + + if (rvalue == 0) return Color(1.0, 1.0, 1.0, 1.0); + return Color( + CLAMP(r / rvalue, 0.0, 1.0), + CLAMP(g / rvalue, 0.0, 1.0), + CLAMP(b / rvalue, 0.0, 1.0), + CLAMP(a / rvalue, 0.0, 1.0)); +} + +void Color::operator/=(const Color &p_color) { + + r = p_color.r == 0 ? 1 : CLAMP(r / p_color.r, 0.0, 1.0); + g = p_color.g == 0 ? 1 : CLAMP(g / p_color.g, 0.0, 1.0); + b = p_color.b == 0 ? 1 : CLAMP(b / p_color.b, 0.0, 1.0); + a = p_color.a == 0 ? 1 : CLAMP(a / p_color.a, 0.0, 1.0); +} + +void Color::operator/=(const real_t &rvalue) { + + if (rvalue == 0) { + r = 1.0; + g = 1.0; + b = 1.0; + a = 1.0; + } else { + r = CLAMP(r / rvalue, 0.0, 1.0); + g = CLAMP(g / rvalue, 0.0, 1.0); + b = CLAMP(b / rvalue, 0.0, 1.0); + a = CLAMP(a / rvalue, 0.0, 1.0); + } +}; + +Color Color::operator-() const { + + return Color( + CLAMP(1.0 - r, 0.0, 1.0), + CLAMP(1.0 - g, 0.0, 1.0), + CLAMP(1.0 - b, 0.0, 1.0), + CLAMP(1.0 - a, 0.0, 1.0)); +} diff --git a/core/color.h b/core/color.h index d3d5db09f9..972b6a1b33 100644 --- a/core/color.h +++ b/core/color.h @@ -67,6 +67,23 @@ struct Color { return components[idx]; } + Color operator+(const Color &p_color) const; + void operator+=(const Color &p_color); + + Color operator-() const; + Color operator-(const Color &p_color) const; + void operator-=(const Color &p_color); + + Color operator*(const Color &p_color) const; + Color operator*(const real_t &rvalue) const; + void operator*=(const Color &p_color); + void operator*=(const real_t &rvalue); + + Color operator/(const Color &p_color) const; + Color operator/(const real_t &rvalue) const; + void operator/=(const Color &p_color); + void operator/=(const real_t &rvalue); + void invert(); void contrast(); Color inverted() const; diff --git a/core/variant_op.cpp b/core/variant_op.cpp index b6e114b853..a11169eb8f 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -493,7 +493,7 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & DEFAULT_OP_FAIL(BASIS); DEFAULT_OP_FAIL(TRANSFORM); - DEFAULT_OP_FAIL(COLOR); + DEFAULT_OP_LOCALMEM(+, COLOR, Color); DEFAULT_OP_FAIL(NODE_PATH); DEFAULT_OP_FAIL(_RID); @@ -549,7 +549,7 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & DEFAULT_OP_FAIL(BASIS); DEFAULT_OP_FAIL(TRANSFORM); - DEFAULT_OP_FAIL(COLOR); + DEFAULT_OP_LOCALMEM(-, COLOR, Color); DEFAULT_OP_FAIL(NODE_PATH); DEFAULT_OP_FAIL(_RID); @@ -645,7 +645,7 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & r_valid = false; return; } break; - DEFAULT_OP_FAIL(COLOR); + DEFAULT_OP_LOCALMEM_NUM(*, COLOR, Color); DEFAULT_OP_FAIL(NODE_PATH); DEFAULT_OP_FAIL(_RID); @@ -717,7 +717,7 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & DEFAULT_OP_FAIL(BASIS); DEFAULT_OP_FAIL(TRANSFORM); - DEFAULT_OP_FAIL(COLOR); + DEFAULT_OP_LOCALMEM_NUM(/, COLOR, Color); DEFAULT_OP_FAIL(NODE_PATH); DEFAULT_OP_FAIL(_RID); @@ -797,7 +797,7 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & DEFAULT_OP_FAIL(BASIS); DEFAULT_OP_FAIL(TRANSFORM); - DEFAULT_OP_FAIL(COLOR); + DEFAULT_OP_LOCALMEM_NEG(COLOR, Color); DEFAULT_OP_FAIL(NODE_PATH); DEFAULT_OP_FAIL(_RID); |