quicktex/src/color.cpp

79 lines
2.5 KiB
C++
Raw Normal View History

2021-02-06 03:44:19 +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
2021-02-07 23:20:03 +00:00
it under the terms of the GNU Lesser General Public License as published by
2021-02-06 03:44:19 +00:00
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
2021-02-07 23:20:03 +00:00
GNU Lesser General Public License for more details.
2021-02-06 03:44:19 +00:00
2021-02-07 23:20:03 +00:00
You should have received a copy of the GNU Lesser General Public License
2021-02-06 03:44:19 +00:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "color.h"
2021-02-06 09:15:23 +00:00
2021-02-06 03:44:19 +00:00
#include <algorithm>
#include <cassert>
2021-02-06 09:15:23 +00:00
#include "util.h"
2021-02-06 03:44:19 +00:00
// region Color32 implementation
Color32::Color32() { Set(0, 0, 0, 0xFF); }
2021-02-06 03:44:19 +00:00
Color32::Color32(uint8_t R, uint8_t G, uint8_t B, uint8_t A) { Set(R, G, B, A); }
2021-02-06 03:44:19 +00:00
uint16_t Color32::Pack565Unscaled(uint16_t R, uint16_t G, uint16_t B) { return B | (G << 5) | (R << 11); }
2021-02-06 03:44:19 +00:00
uint16_t Color32::Pack565(uint16_t R, uint16_t G, uint16_t B) { return Pack565Unscaled(scale8To5(R), scale8To6(G), scale8To5(B)); }
2021-02-06 03:44:19 +00:00
Color32 Color32::Unpack565(uint16_t Packed) {
2021-02-06 03:44:19 +00:00
uint8_t R = scale5To8((Packed >> 11) & 0x1F);
uint8_t G = scale6To8((Packed >> 5) & 0x3F);
uint8_t B = scale5To8(Packed & 0x1F);
return Color32(R, G, B);
}
uint8_t Color32::operator[](uint32_t Index) const {
assert(Index < 4);
return c[Index];
2021-02-06 03:44:19 +00:00
}
uint8_t &Color32::operator[](uint32_t Index) {
assert(Index < 4);
return c[Index];
2021-02-06 03:44:19 +00:00
}
void Color32::Set(uint8_t R, uint8_t G, uint8_t B, uint8_t A) {
this->r = R;
this->g = G;
this->b = B;
this->a = A;
2021-02-06 03:44:19 +00:00
}
void Color32::Set(const Color32 &Other) {
this->r = Other.r;
this->g = Other.g;
this->b = Other.b;
this->a = Other.a;
2021-02-06 03:44:19 +00:00
}
Color32 Color32::min(const Color32 &a, const Color32 &b) {
return Color32(std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]), std::min(a[3], b[3]));
}
Color32 Color32::max(const Color32 &a, const Color32 &b) {
return Color32(std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]), std::max(a[3], b[3]));
}
uint16_t Color32::pack565() { return Pack565(r, g, b); }
2021-02-06 03:44:19 +00:00
uint16_t Color32::pack565Unscaled() { return Pack565Unscaled(r, g, b); }
2021-02-06 03:44:19 +00:00
// endregion