| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Math/Color.h"
- #include <cstdio>
- #include "../DebugNew.h"
- namespace Atomic
- {
- unsigned Color::ToUInt() const
- {
- unsigned r = (unsigned)Clamp(((int)(r_ * 255.0f)), 0, 255);
- unsigned g = (unsigned)Clamp(((int)(g_ * 255.0f)), 0, 255);
- unsigned b = (unsigned)Clamp(((int)(b_ * 255.0f)), 0, 255);
- unsigned a = (unsigned)Clamp(((int)(a_ * 255.0f)), 0, 255);
- return (a << 24) | (b << 16) | (g << 8) | r;
- }
- Vector3 Color::ToHSL() const
- {
- float min, max;
- Bounds(&min, &max, true);
- float h = Hue(min, max);
- float s = SaturationHSL(min, max);
- float l = (max + min) * 0.5f;
- return Vector3(h, s, l);
- }
- Vector3 Color::ToHSV() const
- {
- float min, max;
- Bounds(&min, &max, true);
- float h = Hue(min, max);
- float s = SaturationHSV(min, max);
- float v = max;
- return Vector3(h, s, v);
- }
- void Color::FromHSL(float h, float s, float l, float a)
- {
- float c;
- if (l < 0.5f)
- c = (1.0f + (2.0f * l - 1.0f)) * s;
- else
- c = (1.0f - (2.0f * l - 1.0f)) * s;
- float m = l - 0.5f * c;
- FromHCM(h, c, m);
- a_ = a;
- }
- void Color::FromHSV(float h, float s, float v, float a)
- {
- float c = v * s;
- float m = v - c;
- FromHCM(h, c, m);
- a_ = a;
- }
- float Color::Chroma() const
- {
- float min, max;
- Bounds(&min, &max, true);
- return max - min;
- }
- float Color::Hue() const
- {
- float min, max;
- Bounds(&min, &max, true);
- return Hue(min, max);
- }
- float Color::SaturationHSL() const
- {
- float min, max;
- Bounds(&min, &max, true);
- return SaturationHSL(min, max);
- }
- float Color::SaturationHSV() const
- {
- float min, max;
- Bounds(&min, &max, true);
- return SaturationHSV(min, max);
- }
- float Color::Lightness() const
- {
- float min, max;
- Bounds(&min, &max, true);
- return (max + min) * 0.5f;
- }
- void Color::Bounds(float* min, float* max, bool clipped) const
- {
- assert(min && max);
- if (r_ > g_)
- {
- if (g_ > b_) // r > g > b
- {
- *max = r_;
- *min = b_;
- }
- else // r > g && g <= b
- {
- *max = r_ > b_ ? r_ : b_;
- *min = g_;
- }
- }
- else
- {
- if (b_ > g_) // r <= g < b
- {
- *max = b_;
- *min = r_;
- }
- else // r <= g && b <= g
- {
- *max = g_;
- *min = r_ < b_ ? r_ : b_;
- }
- }
- if (clipped)
- {
- *max = *max > 1.0f ? 1.0f : (*max < 0.0f ? 0.0f : *max);
- *min = *min > 1.0f ? 1.0f : (*min < 0.0f ? 0.0f : *min);
- }
- }
- float Color::MaxRGB() const
- {
- if (r_ > g_)
- return (r_ > b_) ? r_ : b_;
- else
- return (g_ > b_) ? g_ : b_;
- }
- float Color::MinRGB() const
- {
- if (r_ < g_)
- return (r_ < b_) ? r_ : b_;
- else
- return (g_ < b_) ? g_ : b_;
- }
- float Color::Range() const
- {
- float min, max;
- Bounds(&min, &max);
- return max - min;
- }
- void Color::Clip(bool clipAlpha)
- {
- r_ = (r_ > 1.0f) ? 1.0f : ((r_ < 0.0f) ? 0.0f : r_);
- g_ = (g_ > 1.0f) ? 1.0f : ((g_ < 0.0f) ? 0.0f : g_);
- b_ = (b_ > 1.0f) ? 1.0f : ((b_ < 0.0f) ? 0.0f : b_);
- if (clipAlpha)
- a_ = (a_ > 1.0f) ? 1.0f : ((a_ < 0.0f) ? 0.0f : a_);
- }
- void Color::Invert(bool invertAlpha)
- {
- r_ = 1.0f - r_;
- g_ = 1.0f - g_;
- b_ = 1.0f - b_;
- if (invertAlpha)
- a_ = 1.0f - a_;
- }
- Color Color::Lerp(const Color& rhs, float t) const
- {
- float invT = 1.0f - t;
- return Color(
- r_ * invT + rhs.r_ * t,
- g_ * invT + rhs.g_ * t,
- b_ * invT + rhs.b_ * t,
- a_ * invT + rhs.a_ * t
- );
- }
- String Color::ToString() const
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%g %g %g %g", r_, g_, b_, a_);
- return String(tempBuffer);
- }
- float Color::Hue(float min, float max) const
- {
- float chroma = max - min;
- // If chroma equals zero, hue is undefined
- if (chroma <= M_EPSILON)
- return 0.0f;
- // Calculate and return hue
- if (Atomic::Equals(g_, max))
- return (b_ + 2.0f * chroma - r_) / (6.0f * chroma);
- else if (Atomic::Equals(b_, max))
- return (4.0f * chroma - g_ + r_) / (6.0f * chroma);
- else
- {
- float r = (g_ - b_) / (6.0f * chroma);
- return (r < 0.0f) ? 1.0f + r : ((r >= 1.0f) ? r - 1.0f : r);
- }
- }
- float Color::SaturationHSV(float min, float max) const
- {
- // Avoid div-by-zero: result undefined
- if (max <= M_EPSILON)
- return 0.0f;
- // Saturation equals chroma:value ratio
- return 1.0f - (min / max);
- }
- float Color::SaturationHSL(float min, float max) const
- {
- // Avoid div-by-zero: result undefined
- if (max <= M_EPSILON || min >= 1.0f - M_EPSILON)
- return 0.0f;
- // Chroma = max - min, lightness = (max + min) * 0.5
- float hl = (max + min);
- if (hl <= 1.0f)
- return (max - min) / hl;
- else
- return (min - max) / (hl - 2.0f);
- }
- void Color::FromHCM(float h, float c, float m)
- {
- if (h < 0.0f || h >= 1.0f)
- h -= floorf(h);
- float hs = h * 6.0f;
- float x = c * (1.0f - Atomic::Abs(fmodf(hs, 2.0f) - 1.0f));
- // Reconstruct r', g', b' from hue
- if (hs < 2.0f)
- {
- b_ = 0.0f;
- if (hs < 1.0f)
- {
- g_ = x;
- r_ = c;
- }
- else
- {
- g_ = c;
- r_ = x;
- }
- }
- else if (hs < 4.0f)
- {
- r_ = 0.0f;
- if (hs < 3.0f)
- {
- g_ = c;
- b_ = x;
- }
- else
- {
- g_ = x;
- b_ = c;
- }
- }
- else
- {
- g_ = 0.0f;
- if (hs < 5.0f)
- {
- r_ = x;
- b_ = c;
- }
- else
- {
- r_ = c;
- b_ = x;
- }
- }
- r_ += m;
- g_ += m;
- b_ += m;
- }
- /// ATOMIC BEGIN
- unsigned Color::ToUIntArgb() const
- {
- unsigned r = (unsigned)Clamp(((int)(r_ * 255.0f)), 0, 255);
- unsigned g = (unsigned)Clamp(((int)(g_ * 255.0f)), 0, 255);
- unsigned b = (unsigned)Clamp(((int)(b_ * 255.0f)), 0, 255);
- unsigned a = (unsigned)Clamp(((int)(a_ * 255.0f)), 0, 255);
- return (a << 24) | (r << 16) | (g << 8) | b;
- }
- /// ATOMIC END
- const Color Color::WHITE;
- const Color Color::GRAY(0.5f, 0.5f, 0.5f);
- const Color Color::BLACK(0.0f, 0.0f, 0.0f);
- const Color Color::RED(1.0f, 0.0f, 0.0f);
- const Color Color::GREEN(0.0f, 1.0f, 0.0f);
- const Color Color::BLUE(0.0f, 0.0f, 1.0f);
- const Color Color::CYAN(0.0f, 1.0f, 1.0f);
- const Color Color::MAGENTA(1.0f, 0.0f, 1.0f);
- const Color Color::YELLOW(1.0f, 1.0f, 0.0f);
- const Color Color::TRANSPARENT(0.0f, 0.0f, 0.0f, 0.0f);
- }
|