# Compile time hexcolor support ported from palladium
This commit is contained in:
@@ -51,7 +51,6 @@ class Example : public Amy::App {
|
||||
Top->Use();
|
||||
dl->DrawTex(Mgr->Get<Amy::Texture>("icon"));
|
||||
dl->DrawRectFilled(Amy::fvec2(50, 0), 48, Amy::Color(255, 255, 255, 160));
|
||||
// Color only at runtime...yet (Palladium 0.6.0 dev is targeting this)
|
||||
dl->DrawCircleFilled(Amy::fvec2(200, 120), 50, Amy::Color("#ffffff"), 40);
|
||||
dl->DrawSolid();
|
||||
dl->DrawRectFilled(0, 50, Amy::Color(0.f, 1.f, 0.f, 1.f));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <amethyst/types.hpp>
|
||||
#include <amethyst/utils.hpp>
|
||||
|
||||
// We all know where the code is from ... RenderD7 -> Palladium/Amy
|
||||
|
||||
@@ -47,14 +48,27 @@ class Color {
|
||||
* Constructor for Hex Input
|
||||
* @param hex Hex String in `#ffffff` or `#ffffffff` format
|
||||
*/
|
||||
Color(const std::string& hex) { Hex(hex); }
|
||||
constexpr Color(const std::string& hex) { Hex(hex); }
|
||||
|
||||
/**
|
||||
* 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);
|
||||
constexpr Color& Hex(const std::string& hex) {
|
||||
if (!(hex.length() == 7 || hex.length() == 9)) {
|
||||
throw "[PD] Color: hex string is not rgb or rgba!";
|
||||
}
|
||||
r = Utils::HexChar2Int(hex[1]) * 16 + Utils::HexChar2Int(hex[2]);
|
||||
g = Utils::HexChar2Int(hex[3]) * 16 + Utils::HexChar2Int(hex[4]);
|
||||
b = Utils::HexChar2Int(hex[5]) * 16 + Utils::HexChar2Int(hex[6]);
|
||||
if (hex.length() == 9) {
|
||||
a = Utils::HexChar2Int(hex[7]) * 16 + Utils::HexChar2Int(hex[8]);
|
||||
} else {
|
||||
a = 255;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Convert this Color Object to Hex string
|
||||
* @param rgba [default false] sets if 8 or 6 digit color should be returned
|
||||
|
||||
@@ -8,6 +8,13 @@ class Error : public std::runtime_error {
|
||||
Error(ksr str) : std::runtime_error("[amy] " + str) {}
|
||||
};
|
||||
namespace Utils {
|
||||
constexpr int HexChar2Int(char c) {
|
||||
/** Imagine man hat ne lookup table dafür verwendet :/ */
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
|
||||
if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
|
||||
return -1; // Error
|
||||
}
|
||||
vec<uc> LoadFile2Mem(ksr path);
|
||||
str FormatBytes(ull bytes);
|
||||
ui HashMemory(kvr<uc> data);
|
||||
|
||||
@@ -1,40 +1,6 @@
|
||||
#include <amethyst/color.hpp>
|
||||
|
||||
namespace Amy {
|
||||
// The Solution of the biggest performance issue
|
||||
// A Simple Lookup table
|
||||
static const std::unordered_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}};
|
||||
|
||||
Color& Color::Hex(const std::string& hex) {
|
||||
// zu dumm nen safetey check zu schreiben wadafuk
|
||||
/**#ifndef AMY_GOD_DEV
|
||||
/// Safetey check (not required if you just program 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);
|
||||
r = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
g = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
b = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
if (hex.length() == 9) {
|
||||
a = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
} else {
|
||||
a = 255;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string Color::Hex(bool rgba) const {
|
||||
/** Need to int cast (so it is used as num and not char...) */
|
||||
std::stringstream s;
|
||||
|
||||
Reference in New Issue
Block a user