|
|
|
|
#pragma region License
|
|
|
|
|
/*
|
|
|
|
|
License (OLC-3)
|
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Copyright 2018 - 2024 OneLoneCoder.com
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
1. Redistributions or derivations of source code must retain the above copyright
|
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
|
|
2. Redistributions or derivative works in binary form must reproduce the above
|
|
|
|
|
copyright notice. This list of conditions and the following disclaimer must be
|
|
|
|
|
reproduced in the documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors may
|
|
|
|
|
be used to endorse or promote products derived from this software without specific
|
|
|
|
|
prior written permission.
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
|
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
|
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
Portions of this software are copyright <EFBFBD> 2024 The FreeType
|
|
|
|
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
Links
|
|
|
|
|
~~~~~
|
|
|
|
|
YouTube: https://www.youtube.com/javidx9
|
|
|
|
|
https://www.youtube.com/javidx9extra
|
|
|
|
|
Discord: https://discord.gg/WhwHUMV
|
|
|
|
|
Twitter: https://www.twitter.com/javidx9
|
|
|
|
|
Twitch: https://www.twitch.tv/javidx9
|
|
|
|
|
GitHub: https://www.github.com/onelonecoder
|
|
|
|
|
Homepage: https://www.onelonecoder.com
|
|
|
|
|
Patreon: https://www.patreon.com/javidx9
|
|
|
|
|
Community: https://community.onelonecoder.com
|
|
|
|
|
*/
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
|
|
#include "Pixel.h"
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace olc{
|
|
|
|
|
// O------------------------------------------------------------------------------O
|
|
|
|
|
// | olc::Pixel IMPLEMENTATION |
|
|
|
|
|
// O------------------------------------------------------------------------------O
|
|
|
|
|
#if !defined(OLC_IGNORE_PIXEL)
|
|
|
|
|
Pixel::Pixel()
|
|
|
|
|
{ r = 0; g = 0; b = 0; a = nDefaultAlpha; }
|
|
|
|
|
|
|
|
|
|
Pixel::Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
|
|
|
|
|
{ n = red | (green << 8) | (blue << 16) | (alpha << 24); } // Thanks jarekpelczar
|
|
|
|
|
|
|
|
|
|
Pixel::Pixel(uint32_t hex)
|
|
|
|
|
{ n=((hex&0xFF0000)>>16)|(hex&0x00FF00)|((hex&0x0000FF)<<16)|0xFF000000; }
|
|
|
|
|
|
|
|
|
|
bool Pixel::operator==(const Pixel& p) const
|
|
|
|
|
{ return n == p.n; }
|
|
|
|
|
|
|
|
|
|
bool Pixel::operator!=(const Pixel& p) const
|
|
|
|
|
{ return n != p.n; }
|
|
|
|
|
|
|
|
|
|
Pixel Pixel::operator * (const float i) const
|
|
|
|
|
{
|
|
|
|
|
float fR = std::min(255.0f, std::max(0.0f, float(r) * i));
|
|
|
|
|
float fG = std::min(255.0f, std::max(0.0f, float(g) * i));
|
|
|
|
|
float fB = std::min(255.0f, std::max(0.0f, float(b) * i));
|
|
|
|
|
return Pixel(uint8_t(fR), uint8_t(fG), uint8_t(fB), a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel Pixel::operator / (const float i) const
|
|
|
|
|
{
|
|
|
|
|
float fR = std::min(255.0f, std::max(0.0f, float(r) / i));
|
|
|
|
|
float fG = std::min(255.0f, std::max(0.0f, float(g) / i));
|
|
|
|
|
float fB = std::min(255.0f, std::max(0.0f, float(b) / i));
|
|
|
|
|
return Pixel(uint8_t(fR), uint8_t(fG), uint8_t(fB), a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel& Pixel::operator *=(const float i)
|
|
|
|
|
{
|
|
|
|
|
this->r = uint8_t(std::min(255.0f, std::max(0.0f, float(r) * i)));
|
|
|
|
|
this->g = uint8_t(std::min(255.0f, std::max(0.0f, float(g) * i)));
|
|
|
|
|
this->b = uint8_t(std::min(255.0f, std::max(0.0f, float(b) * i)));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel& Pixel::operator /=(const float i)
|
|
|
|
|
{
|
|
|
|
|
this->r = uint8_t(std::min(255.0f, std::max(0.0f, float(r) / i)));
|
|
|
|
|
this->g = uint8_t(std::min(255.0f, std::max(0.0f, float(g) / i)));
|
|
|
|
|
this->b = uint8_t(std::min(255.0f, std::max(0.0f, float(b) / i)));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel Pixel::operator + (const Pixel& p) const
|
|
|
|
|
{
|
|
|
|
|
uint8_t nR = uint8_t(std::min(255, std::max(0, int(r) + int(p.r))));
|
|
|
|
|
uint8_t nG = uint8_t(std::min(255, std::max(0, int(g) + int(p.g))));
|
|
|
|
|
uint8_t nB = uint8_t(std::min(255, std::max(0, int(b) + int(p.b))));
|
|
|
|
|
return Pixel(nR, nG, nB, a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel Pixel::operator - (const Pixel& p) const
|
|
|
|
|
{
|
|
|
|
|
uint8_t nR = uint8_t(std::min(255, std::max(0, int(r) - int(p.r))));
|
|
|
|
|
uint8_t nG = uint8_t(std::min(255, std::max(0, int(g) - int(p.g))));
|
|
|
|
|
uint8_t nB = uint8_t(std::min(255, std::max(0, int(b) - int(p.b))));
|
|
|
|
|
return Pixel(nR, nG, nB, a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel& Pixel::operator += (const Pixel& p)
|
|
|
|
|
{
|
|
|
|
|
this->r = uint8_t(std::min(255, std::max(0, int(r) + int(p.r))));
|
|
|
|
|
this->g = uint8_t(std::min(255, std::max(0, int(g) + int(p.g))));
|
|
|
|
|
this->b = uint8_t(std::min(255, std::max(0, int(b) + int(p.b))));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel& Pixel::operator -= (const Pixel& p) // Thanks Au Lit
|
|
|
|
|
{
|
|
|
|
|
this->r = uint8_t(std::min(255, std::max(0, int(r) - int(p.r))));
|
|
|
|
|
this->g = uint8_t(std::min(255, std::max(0, int(g) - int(p.g))));
|
|
|
|
|
this->b = uint8_t(std::min(255, std::max(0, int(b) - int(p.b))));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel Pixel::operator * (const Pixel& p) const
|
|
|
|
|
{
|
|
|
|
|
uint8_t nR = uint8_t(std::min(255.0f, std::max(0.0f, float(r) * float(p.r) / 255.0f)));
|
|
|
|
|
uint8_t nG = uint8_t(std::min(255.0f, std::max(0.0f, float(g) * float(p.g) / 255.0f)));
|
|
|
|
|
uint8_t nB = uint8_t(std::min(255.0f, std::max(0.0f, float(b) * float(p.b) / 255.0f)));
|
|
|
|
|
uint8_t nA = uint8_t(std::min(255.0f, std::max(0.0f, float(a) * float(p.a) / 255.0f)));
|
|
|
|
|
return Pixel(nR, nG, nB, nA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel& Pixel::operator *=(const Pixel& p)
|
|
|
|
|
{
|
|
|
|
|
this->r = uint8_t(std::min(255.0f, std::max(0.0f, float(r) * float(p.r) / 255.0f)));
|
|
|
|
|
this->g = uint8_t(std::min(255.0f, std::max(0.0f, float(g) * float(p.g) / 255.0f)));
|
|
|
|
|
this->b = uint8_t(std::min(255.0f, std::max(0.0f, float(b) * float(p.b) / 255.0f)));
|
|
|
|
|
this->a = uint8_t(std::min(255.0f, std::max(0.0f, float(a) * float(p.a) / 255.0f)));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel Pixel::inv() const
|
|
|
|
|
{
|
|
|
|
|
uint8_t nR = uint8_t(std::min(255, std::max(0, 255 - int(r))));
|
|
|
|
|
uint8_t nG = uint8_t(std::min(255, std::max(0, 255 - int(g))));
|
|
|
|
|
uint8_t nB = uint8_t(std::min(255, std::max(0, 255 - int(b))));
|
|
|
|
|
return Pixel(nR, nG, nB, a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pixel PixelF(float red, float green, float blue, float alpha)
|
|
|
|
|
{ return Pixel(uint8_t(red * 255.0f), uint8_t(green * 255.0f), uint8_t(blue * 255.0f), uint8_t(alpha * 255.0f)); }
|
|
|
|
|
|
|
|
|
|
Pixel PixelLerp(const olc::Pixel& p1, const olc::Pixel& p2, float t)
|
|
|
|
|
{ return (p2 * t) + p1 * (1.0f - t); }
|
|
|
|
|
|
|
|
|
|
Pixel PixelRaw(uint32_t n){
|
|
|
|
|
Pixel newPixel{};
|
|
|
|
|
newPixel.n=n;
|
|
|
|
|
return newPixel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|