Compare commits
3 Commits
f75f7067ff
...
devel040
Author | SHA1 | Date | |
---|---|---|---|
963fa72e41 | |||
271defffca | |||
ea76a304d4 |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "backends/desktop/glfw"]
|
||||
path = backends/desktop/glfw
|
||||
url = https://github.com/glfw/glfw.git
|
Submodule backends/desktop/glfw updated: e7ea71be03...7b6aead9fb
@ -1,231 +1,231 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
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 <pd/core/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* Color class
|
||||
*
|
||||
* - Supports hex input starting with a # and 6 or 8 digits
|
||||
* - Supports rgb(a) 8Bit unsigned number input
|
||||
* - Supports rgb(a) float input from 0.0 to 1.0
|
||||
* - Supports 32Bit input color
|
||||
* @note Safetey checks are disabled for maximum performance
|
||||
*/
|
||||
class PD_CORE_API Color {
|
||||
private:
|
||||
/** Red Value */
|
||||
u8 m_r;
|
||||
/** Green Value */
|
||||
u8 m_g;
|
||||
/** Blue Value */
|
||||
u8 m_b;
|
||||
/** Alpha Value */
|
||||
u8 m_a;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default Constructor (all variables are set to 0)
|
||||
*/
|
||||
// Color() : m_r(0), m_g(0), m_b(0), m_a(0) {}
|
||||
constexpr Color() : m_r(0), m_g(0), m_b(0), m_a(0) {}
|
||||
constexpr ~Color() {}
|
||||
/**
|
||||
* Constructor for 32Bit Color Input
|
||||
* @param color 32Bit Color value
|
||||
*/
|
||||
constexpr Color(u32 color) {
|
||||
m_a = (color >> 24) & 0xff;
|
||||
m_b = (color >> 16) & 0xff;
|
||||
m_g = (color >> 8) & 0xff;
|
||||
m_r = color & 0xff;
|
||||
}
|
||||
/**
|
||||
* Constructor for 8Bit Input
|
||||
* @param r Red Value
|
||||
* @param g Green Value
|
||||
* @param b Blue Value
|
||||
* @param a Optional Alpha Value (Defaults to 255)
|
||||
*/
|
||||
constexpr Color(int r, int g, int b, int a = 255) {
|
||||
m_r = r;
|
||||
m_g = g;
|
||||
m_b = b;
|
||||
m_a = a;
|
||||
}
|
||||
/**
|
||||
* Constructor for float Input
|
||||
* @param r Red Value
|
||||
* @param g Green Value
|
||||
* @param b Blue Value
|
||||
* @param a Optional Alpha Value (Defaults to 1.0f)
|
||||
* @note There is no Check if the number is between 0.0 and 1.0
|
||||
*/
|
||||
constexpr Color(float r, float g, float b, float a = 1.f) {
|
||||
m_r = static_cast<u8>(255.f * r);
|
||||
m_g = static_cast<u8>(255.f * g);
|
||||
m_b = static_cast<u8>(255.f * b);
|
||||
m_a = static_cast<u8>(255.f * a);
|
||||
}
|
||||
/**
|
||||
* Constructor for Hex Input
|
||||
* @param hex Hex String in `#ffffff` or `#ffffffff` format
|
||||
*/
|
||||
Color(const std::string& hex) { Hex(hex); }
|
||||
/**
|
||||
* Unused Deconstructor
|
||||
*/
|
||||
// ~Color() {}
|
||||
|
||||
/**
|
||||
* Create Color Object by Hex String
|
||||
* @param hex Hex String in `#ffffff` or `#ffffffff` format
|
||||
* @return Color class itself
|
||||
*/
|
||||
Color& Hex(const std::string& hex);
|
||||
/**
|
||||
* Convert this Color Object to Hex string
|
||||
* @param rgba [default false] sets if 8 or 6 digit color should be returned
|
||||
* @return Color Hex String
|
||||
*/
|
||||
std::string Hex(bool rgba = false) const;
|
||||
|
||||
/**
|
||||
* Setter for Red
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& r(u8 v) {
|
||||
m_r = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Red
|
||||
* @return Red Value
|
||||
*/
|
||||
u8 r() const { return m_r; }
|
||||
/**
|
||||
* Setter for Green
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& g(u8 v) {
|
||||
m_g = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Green
|
||||
* @return Green Value
|
||||
*/
|
||||
u8 g() const { return m_r; }
|
||||
/**
|
||||
* Setter for Blue
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& b(u8 v) {
|
||||
m_b = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Blue
|
||||
* @return Blue Value
|
||||
*/
|
||||
u8 b() const { return m_r; }
|
||||
/**
|
||||
* Setter for Alpha
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& a(u8 v) {
|
||||
m_a = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Alpha
|
||||
* @return Alpha Value
|
||||
*/
|
||||
u8 a() const { return m_r; }
|
||||
|
||||
/**
|
||||
* Fade from Current to another Color
|
||||
* @param color Color to fade to
|
||||
* @param p Amount (supports -1.0 to 1.0 for use of sine)
|
||||
* @return Class Reference
|
||||
*/
|
||||
Color& Fade(const Color& color, float p) {
|
||||
m_a = static_cast<u8>((color.a() - m_a) * ((p + 1.f) / 2));
|
||||
m_b = static_cast<u8>((color.b() - m_b) * ((p + 1.f) / 2));
|
||||
m_g = static_cast<u8>((color.g() - m_g) * ((p + 1.f) / 2));
|
||||
m_r = static_cast<u8>((color.r() - m_r) * ((p + 1.f) / 2));
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Get 32Bit Color Value
|
||||
* @return 32Bit Color Value
|
||||
*/
|
||||
u32 Get() const { return (m_a << 24) | (m_b << 16) | (m_g << 8) | m_r; }
|
||||
/**
|
||||
* Get The Luminance of the Color
|
||||
* @return luminance (from 0.0 to 1.0)
|
||||
*/
|
||||
float Luminance() const {
|
||||
// For Reference https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
|
||||
return (0.3 * (m_r / 255.f) + 0.59 * (m_g / 255.f) + 0.11 * (m_b / 255.f));
|
||||
}
|
||||
/**
|
||||
* Check if the Color is Light or Dark
|
||||
* @return true if light
|
||||
*/
|
||||
bool IsLight() const { return (Luminance() >= 0.5); }
|
||||
|
||||
/**
|
||||
* Operator to cast Color to 32Bit Value
|
||||
* @return 32Bit Color Value
|
||||
*/
|
||||
operator u32() const { return Get(); }
|
||||
};
|
||||
namespace Colors {
|
||||
constexpr Color White = Color(1.f, 1.f, 1.f, 1.f);
|
||||
constexpr Color Black = Color(0.f, 0.f, 0.f, 1.f);
|
||||
constexpr Color Red = Color(1.f, 0.f, 0.f, 1.f);
|
||||
constexpr Color Green = Color(0.f, 1.f, 0.f, 1.f);
|
||||
constexpr Color Blue = Color(0.f, 0.f, 1.f, 1.f);
|
||||
constexpr Color Yellow = Color(1.f, 1.f, 0.f, 1.f);
|
||||
constexpr Color Cyan = Color(0.f, 1.f, 1.f, 1.f);
|
||||
constexpr Color Magenta = Color(1.f, 0.f, 1.f, 1.f);
|
||||
constexpr Color Gray = Color(0.5f, 0.5f, 0.5f, 1.f);
|
||||
constexpr Color LightGray = Color(0.75f, 0.75f, 0.75f, 1.f);
|
||||
constexpr Color DarkGray = Color(0.25f, 0.25f, 0.25f, 1.f);
|
||||
constexpr Color Orange = Color(1.f, 0.65f, 0.f, 1.f);
|
||||
constexpr Color Pink = Color(1.f, 0.75f, 0.8f, 1.f);
|
||||
constexpr Color Brown = Color(0.6f, 0.4f, 0.2f, 1.f);
|
||||
constexpr Color Purple = Color(0.5f, 0.f, 0.5f, 1.f);
|
||||
constexpr Color Teal = Color(0.f, 0.5f, 0.5f, 1.f);
|
||||
constexpr Color Transparent = Color(0.f, 0.f, 0.f, 0.f);
|
||||
} // namespace Colors
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
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 <pd/core/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
/**
|
||||
* Color class
|
||||
*
|
||||
* - Supports hex input starting with a # and 6 or 8 digits
|
||||
* - Supports rgb(a) 8Bit unsigned number input
|
||||
* - Supports rgb(a) float input from 0.0 to 1.0
|
||||
* - Supports 32Bit input color
|
||||
* @note Safetey checks are disabled for maximum performance
|
||||
*/
|
||||
class PD_CORE_API Color {
|
||||
private:
|
||||
/** Red Value */
|
||||
u8 m_r;
|
||||
/** Green Value */
|
||||
u8 m_g;
|
||||
/** Blue Value */
|
||||
u8 m_b;
|
||||
/** Alpha Value */
|
||||
u8 m_a;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default Constructor (all variables are set to 0)
|
||||
*/
|
||||
// Color() : m_r(0), m_g(0), m_b(0), m_a(0) {}
|
||||
constexpr Color() : m_r(0), m_g(0), m_b(0), m_a(0) {}
|
||||
constexpr ~Color() {}
|
||||
/**
|
||||
* Constructor for 32Bit Color Input
|
||||
* @param color 32Bit Color value
|
||||
*/
|
||||
constexpr Color(u32 color) {
|
||||
m_a = (color >> 24) & 0xff;
|
||||
m_b = (color >> 16) & 0xff;
|
||||
m_g = (color >> 8) & 0xff;
|
||||
m_r = color & 0xff;
|
||||
}
|
||||
/**
|
||||
* Constructor for 8Bit Input
|
||||
* @param r Red Value
|
||||
* @param g Green Value
|
||||
* @param b Blue Value
|
||||
* @param a Optional Alpha Value (Defaults to 255)
|
||||
*/
|
||||
constexpr Color(int r, int g, int b, int a = 255) {
|
||||
m_r = r;
|
||||
m_g = g;
|
||||
m_b = b;
|
||||
m_a = a;
|
||||
}
|
||||
/**
|
||||
* Constructor for float Input
|
||||
* @param r Red Value
|
||||
* @param g Green Value
|
||||
* @param b Blue Value
|
||||
* @param a Optional Alpha Value (Defaults to 1.0f)
|
||||
* @note There is no Check if the number is between 0.0 and 1.0
|
||||
*/
|
||||
constexpr Color(float r, float g, float b, float a = 1.f) {
|
||||
m_r = static_cast<u8>(255.f * r);
|
||||
m_g = static_cast<u8>(255.f * g);
|
||||
m_b = static_cast<u8>(255.f * b);
|
||||
m_a = static_cast<u8>(255.f * a);
|
||||
}
|
||||
/**
|
||||
* Constructor for Hex Input
|
||||
* @param hex Hex String in `#ffffff` or `#ffffffff` format
|
||||
*/
|
||||
Color(const std::string& hex) { Hex(hex); }
|
||||
/**
|
||||
* Unused Deconstructor
|
||||
*/
|
||||
// ~Color() {}
|
||||
|
||||
/**
|
||||
* Create Color Object by Hex String
|
||||
* @param hex Hex String in `#ffffff` or `#ffffffff` format
|
||||
* @return Color class itself
|
||||
*/
|
||||
Color& Hex(const std::string& hex);
|
||||
/**
|
||||
* Convert this Color Object to Hex string
|
||||
* @param rgba [default false] sets if 8 or 6 digit color should be returned
|
||||
* @return Color Hex String
|
||||
*/
|
||||
std::string Hex(bool rgba = false) const;
|
||||
|
||||
/**
|
||||
* Setter for Red
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& r(u8 v) {
|
||||
m_r = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Red
|
||||
* @return Red Value
|
||||
*/
|
||||
u8 r() const { return m_r; }
|
||||
/**
|
||||
* Setter for Green
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& g(u8 v) {
|
||||
m_g = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Green
|
||||
* @return Green Value
|
||||
*/
|
||||
u8 g() const { return m_g; }
|
||||
/**
|
||||
* Setter for Blue
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& b(u8 v) {
|
||||
m_b = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Blue
|
||||
* @return Blue Value
|
||||
*/
|
||||
u8 b() const { return m_b; }
|
||||
/**
|
||||
* Setter for Alpha
|
||||
* @param v value
|
||||
* @return Color class reference
|
||||
*/
|
||||
Color& a(u8 v) {
|
||||
m_a = v;
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Getter for Alpha
|
||||
* @return Alpha Value
|
||||
*/
|
||||
u8 a() const { return m_a; }
|
||||
|
||||
/**
|
||||
* Fade from Current to another Color
|
||||
* @param color Color to fade to
|
||||
* @param p Amount (supports -1.0 to 1.0 for use of sine)
|
||||
* @return Class Reference
|
||||
*/
|
||||
Color& Fade(const Color& color, float p) {
|
||||
m_a = static_cast<u8>((color.a() - m_a) * ((p + 1.f) / 2));
|
||||
m_b = static_cast<u8>((color.b() - m_b) * ((p + 1.f) / 2));
|
||||
m_g = static_cast<u8>((color.g() - m_g) * ((p + 1.f) / 2));
|
||||
m_r = static_cast<u8>((color.r() - m_r) * ((p + 1.f) / 2));
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Get 32Bit Color Value
|
||||
* @return 32Bit Color Value
|
||||
*/
|
||||
u32 Get() const { return (m_a << 24) | (m_b << 16) | (m_g << 8) | m_r; }
|
||||
/**
|
||||
* Get The Luminance of the Color
|
||||
* @return luminance (from 0.0 to 1.0)
|
||||
*/
|
||||
float Luminance() const {
|
||||
// For Reference https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
|
||||
return (0.3 * (m_r / 255.f) + 0.59 * (m_g / 255.f) + 0.11 * (m_b / 255.f));
|
||||
}
|
||||
/**
|
||||
* Check if the Color is Light or Dark
|
||||
* @return true if light
|
||||
*/
|
||||
bool IsLight() const { return (Luminance() >= 0.5); }
|
||||
|
||||
/**
|
||||
* Operator to cast Color to 32Bit Value
|
||||
* @return 32Bit Color Value
|
||||
*/
|
||||
operator u32() const { return Get(); }
|
||||
};
|
||||
namespace Colors {
|
||||
constexpr Color White = Color(1.f, 1.f, 1.f, 1.f);
|
||||
constexpr Color Black = Color(0.f, 0.f, 0.f, 1.f);
|
||||
constexpr Color Red = Color(1.f, 0.f, 0.f, 1.f);
|
||||
constexpr Color Green = Color(0.f, 1.f, 0.f, 1.f);
|
||||
constexpr Color Blue = Color(0.f, 0.f, 1.f, 1.f);
|
||||
constexpr Color Yellow = Color(1.f, 1.f, 0.f, 1.f);
|
||||
constexpr Color Cyan = Color(0.f, 1.f, 1.f, 1.f);
|
||||
constexpr Color Magenta = Color(1.f, 0.f, 1.f, 1.f);
|
||||
constexpr Color Gray = Color(0.5f, 0.5f, 0.5f, 1.f);
|
||||
constexpr Color LightGray = Color(0.75f, 0.75f, 0.75f, 1.f);
|
||||
constexpr Color DarkGray = Color(0.25f, 0.25f, 0.25f, 1.f);
|
||||
constexpr Color Orange = Color(1.f, 0.65f, 0.f, 1.f);
|
||||
constexpr Color Pink = Color(1.f, 0.75f, 0.8f, 1.f);
|
||||
constexpr Color Brown = Color(0.6f, 0.4f, 0.2f, 1.f);
|
||||
constexpr Color Purple = Color(0.5f, 0.f, 0.5f, 1.f);
|
||||
constexpr Color Teal = Color(0.f, 0.5f, 0.5f, 1.f);
|
||||
constexpr Color Transparent = Color(0.f, 0.f, 0.f, 0.f);
|
||||
} // namespace Colors
|
||||
} // namespace PD
|
@ -30,6 +30,12 @@ namespace PD {
|
||||
* Set of File Functions
|
||||
*/
|
||||
namespace IO {
|
||||
enum RleFmt {
|
||||
Default = 0,
|
||||
_16 = 1 << 0,
|
||||
_32 = 1 << 1,
|
||||
_64 = 1 << 2,
|
||||
};
|
||||
/**
|
||||
* Load a File into an 8Bit Memory Buffer
|
||||
* @param path Path to the File
|
||||
@ -42,5 +48,25 @@ PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path);
|
||||
* @return 32Bit Hash
|
||||
*/
|
||||
PD_CORE_API u32 HashMemory(const std::vector<u8>& data);
|
||||
/**
|
||||
* Function to decrompress RLE buffer
|
||||
* @param data Data buffer to decompress
|
||||
*/
|
||||
PD_CORE_API void DecompressRLE(std::vector<u8>& data);
|
||||
/**
|
||||
* Function to decrompress Extended RLE Buffer
|
||||
* @param data Data buffer to decompress
|
||||
*/
|
||||
PD_CORE_API void DecompressRLE_Ex(std::vector<u8>& data);
|
||||
/**
|
||||
* Function to compress data with RLE Algorithm
|
||||
* @param data Data buf
|
||||
*/
|
||||
PD_CORE_API void CompressRLE(std::vector<u8>& data);
|
||||
/**
|
||||
* Extended RLE Compress function (slower cause searches best format)
|
||||
* @param data Data buf
|
||||
*/
|
||||
PD_CORE_API void CompressRLE_Ex(std::vector<u8>& data);
|
||||
} // namespace IO
|
||||
} // namespace PD
|
@ -1,72 +1,73 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
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 <pd/core/color.hpp>
|
||||
|
||||
namespace PD {
|
||||
// The Solution of the biggest performance issue
|
||||
// A Simple Lookup table
|
||||
static const std::map<char, int> HEX_DEC = {
|
||||
{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5},
|
||||
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'a', 10}, {'b', 11},
|
||||
{'c', 12}, {'d', 13}, {'e', 14}, {'f', 15}, {'A', 10}, {'B', 11},
|
||||
{'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
|
||||
|
||||
PD_CORE_API Color& Color::Hex(const std::string& hex) {
|
||||
#ifdef PD_NO_SAFE_CODE
|
||||
/// Safetey check (not required if you programm well xd)
|
||||
if (hex.length() != 7 || hex.length() != 9 || hex.length() != 6 ||
|
||||
hex.length() != 8 || std::find_if(hex.begin(), hex.end(), [](char c) {
|
||||
return !std::isxdigit(c);
|
||||
}) != hex.end()) {
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
int offset = ((hex.length() == 7 || hex.length() == 9) ? 1 : 0);
|
||||
m_r = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_g = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_b = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
if (hex.length() == 9) {
|
||||
m_a = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
} else {
|
||||
m_a = 255;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
PD_CORE_API std::string Color::Hex(bool rgba) const {
|
||||
std::stringstream s;
|
||||
s << "#";
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << m_r;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << m_g;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << m_b;
|
||||
if (rgba) {
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << m_a;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
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 <pd/core/color.hpp>
|
||||
|
||||
namespace PD {
|
||||
// The Solution of the biggest performance issue
|
||||
// A Simple Lookup table
|
||||
static const std::map<char, int> HEX_DEC = {
|
||||
{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5},
|
||||
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'a', 10}, {'b', 11},
|
||||
{'c', 12}, {'d', 13}, {'e', 14}, {'f', 15}, {'A', 10}, {'B', 11},
|
||||
{'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
|
||||
|
||||
PD_CORE_API Color& Color::Hex(const std::string& hex) {
|
||||
#ifdef PD_NO_SAFE_CODE
|
||||
/// Safetey check (not required if you programm well xd)
|
||||
if (hex.length() != 7 || hex.length() != 9 || hex.length() != 6 ||
|
||||
hex.length() != 8 || std::find_if(hex.begin(), hex.end(), [](char c) {
|
||||
return !std::isxdigit(c);
|
||||
}) != hex.end()) {
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
int offset = ((hex.length() == 7 || hex.length() == 9) ? 1 : 0);
|
||||
m_r = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_g = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_b = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
if (hex.length() == 9) {
|
||||
m_a = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
} else {
|
||||
m_a = 255;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
PD_CORE_API std::string Color::Hex(bool rgba) const {
|
||||
/** Need to int cast (so it is used as num and not char...) */
|
||||
std::stringstream s;
|
||||
s << "#";
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_r;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_g;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_b;
|
||||
if (rgba) {
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_a;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
} // namespace PD
|
@ -23,6 +23,74 @@ SOFTWARE.
|
||||
|
||||
#include <pd/core/io.hpp>
|
||||
|
||||
PD_CORE_API void DecompressRLE16(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; i < cpy.size(); i += 3) {
|
||||
for (size_t j = 0; j < cpy[i + 2]; j++) {
|
||||
data.push_back(cpy[i]);
|
||||
data.push_back(cpy[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE32(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; (i + 4) < cpy.size(); i += 5) {
|
||||
for (size_t j = 0; j < cpy[i + 4]; j++) {
|
||||
data.push_back(cpy[i]);
|
||||
data.push_back(cpy[i + 1]);
|
||||
data.push_back(cpy[i + 2]);
|
||||
data.push_back(cpy[i + 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE16(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
size_t i = 0;
|
||||
while ((i + 1) < cpy.size()) {
|
||||
PD::u16 v = PD::u16(cpy[i]) | (PD::u16(cpy[i + 1]) << 8);
|
||||
size_t c = 1;
|
||||
while ((i + c * 2 + 1) < cpy.size() &&
|
||||
(PD::u16(cpy[i + c * 2]) | (PD::u16(cpy[i + c * 2 + 1])) << 8) ==
|
||||
v &&
|
||||
c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(PD::u8(v & 0xFF));
|
||||
data.push_back(PD::u8((v >> 8) & 0xFF));
|
||||
data.push_back(c);
|
||||
i += c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE32(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
size_t i = 0;
|
||||
while ((i + 3) < cpy.size()) {
|
||||
PD::u32 v = PD::u32(cpy[i]) | (PD::u32(cpy[i + 1]) << 8) |
|
||||
(PD::u32(cpy[i + 2]) << 16) | (PD::u32(cpy[i + 3]) << 24);
|
||||
size_t c = 1;
|
||||
while ((i + c * 4 + 3) < cpy.size() &&
|
||||
(PD::u32(cpy[i + c * 4]) | (PD::u32(cpy[i + c * 4 + 1]) << 8) |
|
||||
(PD::u32(cpy[i + c * 4 + 2]) << 16) |
|
||||
(PD::u32(cpy[i + c * 4 + 3]) << 24)) == v &&
|
||||
c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(PD::u8(v & 0xFF));
|
||||
data.push_back(PD::u8((v >> 8) & 0xFF));
|
||||
data.push_back(PD::u8((v >> 16) & 0xFF));
|
||||
data.push_back(PD::u8((v >> 24) & 0xFF));
|
||||
data.push_back(c);
|
||||
i += c * 4;
|
||||
}
|
||||
}
|
||||
|
||||
namespace PD {
|
||||
namespace IO {
|
||||
PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path) {
|
||||
@ -38,6 +106,7 @@ PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path) {
|
||||
iff.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
PD_CORE_API u32 HashMemory(const std::vector<u8>& data) {
|
||||
u32 hash = 4477;
|
||||
for (auto& it : data) {
|
||||
@ -45,5 +114,78 @@ PD_CORE_API u32 HashMemory(const std::vector<u8>& data) {
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE(std::vector<u8>& data) {
|
||||
if ((data.size() % 2) != 0) {
|
||||
return;
|
||||
}
|
||||
std::vector<u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; i < cpy.size(); i += 2) {
|
||||
data.insert(data.end(), cpy[i + 1], cpy[i]);
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE_Ex(std::vector<u8>& data) {
|
||||
if (!data.size()) {
|
||||
return;
|
||||
}
|
||||
u8 fmt = data[0];
|
||||
data.erase(data.begin());
|
||||
if (fmt == 0) {
|
||||
DecompressRLE(data);
|
||||
} else if (fmt == 1) {
|
||||
DecompressRLE16(data);
|
||||
} else if (fmt == 2) {
|
||||
DecompressRLE32(data);
|
||||
}
|
||||
/** unknown returns input data */
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE(std::vector<u8>& data) {
|
||||
if (data.empty()) {
|
||||
/** No exceptions enabled :( */
|
||||
return;
|
||||
}
|
||||
std::vector<u8> cpy = data;
|
||||
data.clear();
|
||||
/** 8-Bit RLE */
|
||||
data.push_back(0);
|
||||
size_t i = 0;
|
||||
while (i < cpy.size()) {
|
||||
u8 v = cpy[i];
|
||||
u8 c = 1;
|
||||
while (i + c < cpy.size() && cpy[i + c] == v && c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(v);
|
||||
data.push_back(c);
|
||||
i += c;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE_Ex(std::vector<u8>& data) {
|
||||
if (data.empty()) {
|
||||
/** No exceptions enabled :( */
|
||||
return;
|
||||
}
|
||||
std::vector<u8> _8 = data;
|
||||
std::vector<u8> _16 = data;
|
||||
std::vector<u8> _32 = data;
|
||||
CompressRLE(_8);
|
||||
CompressRLE16(_16);
|
||||
CompressRLE32(_32);
|
||||
if (_16.size() < _8.size() && _16.size() < _32.size()) {
|
||||
_16.insert(_16.begin(), 1);
|
||||
data = _16;
|
||||
return;
|
||||
} else if (_32.size() < _8.size()) {
|
||||
_32.insert(_32.begin(), 2);
|
||||
data = _32;
|
||||
return;
|
||||
}
|
||||
_8.insert(_8.begin(), 0);
|
||||
data = _8;
|
||||
}
|
||||
} // namespace IO
|
||||
} // namespace PD
|
@ -78,22 +78,25 @@ PD_IMAGE_API void Image::Copy(const std::vector<u8>& buf, int w, int h,
|
||||
}
|
||||
|
||||
PD_IMAGE_API void Image::Convert(Image::Ref img, Image::Format dst) {
|
||||
if (img->Fmt() == dst) {
|
||||
if (img->pFmt == dst) {
|
||||
return;
|
||||
} else if (img->Fmt() == Image::RGB && dst == Image::BGR) {
|
||||
} else if (img->pFmt == Image::RGB && dst == Image::BGR) {
|
||||
ImgConvert::ReverseBuf(img->pBuffer, 3, img->pWidth, img->pHeight);
|
||||
} else if (img->Fmt() == Image::RGB && dst == Image::RGBA) {
|
||||
img->pFmt = BGR;
|
||||
} else if (img->pFmt == Image::RGB && dst == Image::RGBA) {
|
||||
std::vector<PD::u8> cpy = img->pBuffer;
|
||||
img->pBuffer.resize(img->pWidth * img->pHeight * 4);
|
||||
ImgConvert::RGB24toRGBA32(img->pBuffer, cpy, img->pWidth, img->pHeight);
|
||||
} else if (img->Fmt() == Image::RGBA && dst == Image::RGB) {
|
||||
img->pFmt = RGBA;
|
||||
} else if (img->pFmt == Image::RGBA && dst == Image::RGB) {
|
||||
std::vector<PD::u8> cpy = img->pBuffer;
|
||||
img->pBuffer.resize(img->pWidth * img->pHeight * 3);
|
||||
ImgConvert::RGB32toRGBA24(img->pBuffer, cpy, img->pWidth, img->pHeight);
|
||||
} else if (img->Fmt() == Image::RGBA && dst == Image::RGB565) {
|
||||
img->pFmt = RGB;
|
||||
} else if (img->pFmt == Image::RGBA && dst == Image::RGB565) {
|
||||
Convert(img, Image::RGB);
|
||||
Convert(img, Image::RGB565);
|
||||
} else if (img->Fmt() == Image::RGB && dst == Image::RGB565) {
|
||||
} else if (img->pFmt == Image::RGB && dst == Image::RGB565) {
|
||||
auto f = [](u8 r, u8 g, u8 b) -> u16 {
|
||||
u16 _r = (r >> 3);
|
||||
u16 _g = (g >> 2);
|
||||
@ -111,6 +114,7 @@ PD_IMAGE_API void Image::Convert(Image::Ref img, Image::Format dst) {
|
||||
img->pBuffer[dst + 1] = new_px & 0xff;
|
||||
}
|
||||
}
|
||||
img->pFmt = RGB565;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user