2021-02-13 08:16:04 +00:00
|
|
|
/* Python-rgbcx Texture Compression Library
|
|
|
|
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
|
|
|
|
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
|
|
|
|
and licenced under the public domain
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "Color.h"
|
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
#include <algorithm> // for max, Min
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
#include "Vector4.h"
|
2021-02-22 07:15:04 +00:00
|
|
|
#include "Vector4Int.h"
|
2021-02-16 07:02:24 +00:00
|
|
|
#include "util.h" // for scale5To8, scale8To5, assert5bit, scale6To8
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
namespace rgbcx {
|
|
|
|
|
2021-02-13 08:16:04 +00:00
|
|
|
Color::Color() { SetRGBA(0, 0, 0, 0xFF); }
|
|
|
|
|
|
|
|
Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { SetRGBA(r, g, b, a); }
|
|
|
|
|
|
|
|
uint16_t Color::Pack565Unscaled(uint8_t r, uint8_t g, uint8_t b) {
|
2021-02-16 07:02:24 +00:00
|
|
|
assert5bit(r);
|
|
|
|
assert6bit(g);
|
|
|
|
assert5bit(b);
|
2021-02-13 08:16:04 +00:00
|
|
|
return static_cast<uint16_t>(b | (g << 5) | (r << 11));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Color::Pack565(uint8_t r, uint8_t g, uint8_t b) { return Pack565Unscaled(scale8To5(r), scale8To6(g), scale8To5(b)); }
|
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
Color Color::Unpack565Unscaled(uint16_t Packed) {
|
|
|
|
uint8_t r = (Packed >> 11) & 0x1F;
|
|
|
|
uint8_t g = (Packed >> 5) & 0x3F;
|
|
|
|
uint8_t b = Packed & 0x1F;
|
|
|
|
|
|
|
|
return Color(r, g, b);
|
|
|
|
}
|
|
|
|
|
2021-02-13 08:16:04 +00:00
|
|
|
Color Color::Unpack565(uint16_t Packed) {
|
|
|
|
uint8_t r = static_cast<uint8_t>(scale5To8((Packed >> 11) & 0x1FU));
|
|
|
|
uint8_t g = static_cast<uint8_t>(scale6To8((Packed >> 5) & 0x3FU));
|
|
|
|
uint8_t b = static_cast<uint8_t>(scale5To8(Packed & 0x1FU));
|
|
|
|
|
|
|
|
return Color(r, g, b);
|
|
|
|
}
|
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
Color Color::PreciseRound565(Vector4 &v) {
|
|
|
|
int trial_r = (int)(v[0] * UINT5_MAX);
|
|
|
|
int trial_g = (int)(v[1] * UINT6_MAX);
|
|
|
|
int trial_b = (int)(v[2] * UINT5_MAX);
|
|
|
|
|
|
|
|
// clamp to prevent weirdness with slightly out of bounds float values
|
|
|
|
uint8_t r = (uint8_t)clampi(trial_r, 0, UINT5_MAX);
|
|
|
|
uint8_t g = (uint8_t)clampi(trial_g, 0, UINT6_MAX);
|
|
|
|
uint8_t b = (uint8_t)clampi(trial_b, 0, UINT5_MAX);
|
|
|
|
|
|
|
|
// increment each channel if above the rounding point
|
|
|
|
r += v[0] > Midpoints5bit[r];
|
|
|
|
g += v[1] > Midpoints6bit[g];
|
|
|
|
b += v[2] > Midpoints5bit[b];
|
|
|
|
|
|
|
|
assert5bit(r);
|
|
|
|
assert6bit(g);
|
|
|
|
assert5bit(b);
|
2021-02-13 08:16:04 +00:00
|
|
|
|
|
|
|
return Color(r, g, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Color::SetRGBA(uint8_t vr, uint8_t vg, uint8_t vb, uint8_t va = 0xFF) {
|
2021-02-13 09:32:15 +00:00
|
|
|
r = vr;
|
|
|
|
g = vg;
|
|
|
|
b = vb;
|
|
|
|
a = va;
|
2021-02-13 08:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Color::SetRGB(uint8_t vr, uint8_t vg, uint8_t vb) {
|
2021-02-13 09:32:15 +00:00
|
|
|
r = vr;
|
|
|
|
g = vg;
|
|
|
|
b = vb;
|
2021-02-13 08:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
size_t Color::MinChannelRGB() {
|
2021-02-22 07:15:04 +00:00
|
|
|
if (r <= g && r <= b) return 0;
|
|
|
|
if (g <= b && g <= r) return 1;
|
2021-02-21 08:57:48 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Color::MaxChannelRGB() {
|
2021-02-22 07:15:04 +00:00
|
|
|
if (r >= g && r >= b) return 0;
|
|
|
|
if (g >= b && g >=r) return 1;
|
2021-02-21 08:57:48 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
Color Color::Min(const Color &A, const Color &B) { return Color(std::min(A[0], B[0]), std::min(A[1], B[1]), std::min(A[2], B[2]), std::min(A[3], B[3])); }
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
Color Color::Max(const Color &a, const Color &b) { return Color(std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]), std::max(a[3], b[3])); }
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-22 07:15:04 +00:00
|
|
|
Color::operator Vector4() const { return Vector4(r, g, b, a); }
|
|
|
|
Color::operator Vector4Int() const { return Vector4Int(r, g, b, a);}
|
2021-02-24 03:44:36 +00:00
|
|
|
Vector4Int operator-(const Color &lhs, const Color &rhs) {
|
|
|
|
Vector4Int result;
|
|
|
|
for (unsigned i = 0; i < 4; i++) {
|
|
|
|
result[i] = (int)lhs[i] - rhs[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-02-22 07:15:04 +00:00
|
|
|
|
|
|
|
uint16_t Color::Pack565() const { return Pack565(r, g, b); }
|
|
|
|
uint16_t Color::Pack565Unscaled() const { return Pack565Unscaled(r, g, b); }
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-13 09:32:15 +00:00
|
|
|
Color Color::ScaleTo565() const { return Color(scale8To5(r), scale8To6(g), scale8To5(b)); }
|
|
|
|
Color Color::ScaleFrom565() const { return Color(scale5To8(r), scale6To8(g), scale5To8(b)); }
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-21 08:57:48 +00:00
|
|
|
} // namespace rgbcx
|