Updated color preview and did some refactoring

Moved the lowercase hex from a bool to a output format and also made the preview match the output format, still need to make it fit in the box
This commit is contained in:
Sepandar 2025-02-19 21:13:35 -08:00
parent 2db91c145a
commit d82346e4fa
No known key found for this signature in database
4 changed files with 82 additions and 20 deletions

View file

@ -9,6 +9,7 @@ project(
VERSION ${VERSION})
set(CMAKE_MESSAGE_LOG_LEVEL "STATUS")
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
add_compile_definitions(HYPRPICKER_VERSION="${VERSION}")

View file

@ -425,17 +425,78 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) {
cairo_clip(PCAIRO);
cairo_paint(PCAIRO);
if (!m_bDisableHexPreview) {
if (!m_bDisablePreview) {
const auto currentColor = getColorFromPixel(pSurface, CLICKPOS);
std::string hexBuffer;
if (m_bUseLowerCase)
hexBuffer = std::format("#{:02x}{:02x}{:02x}", currentColor.r, currentColor.g, currentColor.b);
else
hexBuffer = std::format("#{:02X}{:02X}{:02X}", currentColor.r, currentColor.g, currentColor.b);
std::string previewBuffer;
auto fmax3 = [](float a, float b, float c) -> float { return (a > b && a > c) ? a : (b > c) ? b : c; };
auto fmin3 = [](float a, float b, float c) -> float { return (a < b && a < c) ? a : (b < c) ? b : c; };
switch(m_bSelectedOutputMode) {
case OUTPUT_CMYK: {
float r = 1 - currentColor.r / 255.0f, g = 1 - currentColor.g / 255.0f, b = 1 - currentColor.b / 255.0f;
float k = fmin3(r, g, b), K = (k == 1) ? 1 : 1 - k;
float c = (r - k) / K, m = (g - k) / K, y = (b - k) / K;
c = std::round(c * 100);
m = std::round(m * 100);
y = std::round(y * 100);
k = std::round(k * 100);
previewBuffer = std::format("{}% {}% {}% {}%", c, m, y, k);
break;
}
case OUTPUT_HEX: {
previewBuffer = std::format("#{:02X}{:02X}{:02X}", currentColor.r, currentColor.g, currentColor.b);
break;
}
case OUTPUT_LHEX: {
previewBuffer = std::format("#{:02x}{:02x}{:02x}", currentColor.r, currentColor.g, currentColor.b);
break;
}
case OUTPUT_HSL:
case OUTPUT_HSV: {
auto floatEq = [](float a, float b) -> bool {
return std::nextafter(a, std::numeric_limits<double>::lowest()) <= b && std::nextafter(a, std::numeric_limits<double>::max()) >= b;
};
float h, s, l, v;
float r = currentColor.r / 255.0f, g = currentColor.g / 255.0f, b = currentColor.b / 255.0f;
float max = fmax3(r, g, b), min = fmin3(r, g, b);
float c = max - min;
v = max;
if (c == 0)
h = 0;
else if (v == r)
h = 60 * (0 + (g - b) / c);
else if (v == g)
h = 60 * (2 + (b - r) / c);
else /* v == b */
h = 60 * (4 + (r - g) / c);
float l_or_v;
if (m_bSelectedOutputMode == OUTPUT_HSL) {
l = (max + min) / 2;
s = (floatEq(l, 0.0f) || floatEq(l, 1.0f)) ? 0 : (v - l) / std::min(l, 1 - l);
l_or_v = std::round(l * 100);
} else {
v = max;
s = floatEq(v, 0.0f) ? 0 : c / v;
l_or_v = std::round(v * 100);
}
h = std::round(h < 0 ? h + 360 : h);
s = std::round(s * 100);
previewBuffer = std::format("{} {}% {}%", h, s, l_or_v);
break;
}
case OUTPUT_RGB: {
previewBuffer = std::format("{} {} {}", currentColor.r, currentColor.g, currentColor.b);
break;
}
}
cairo_set_source_rgba(PCAIRO, 0.0, 0.0, 0.0, 0.5);
double x, y, width = 85, height = 28, radius = 6;
double x, y, width = 15+(10*previewBuffer.length()), height = 28, radius = 6;
if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
@ -476,7 +537,7 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) {
else
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
cairo_show_text(PCAIRO, hexBuffer.c_str());
cairo_show_text(PCAIRO, previewBuffer.c_str());
cairo_surface_flush(PBUFFER->surface);
}
@ -650,9 +711,10 @@ void CHyprpicker::initMouse() {
finish();
break;
}
case OUTPUT_HEX: {
case OUTPUT_HEX:
case OUTPUT_LHEX: {
auto toHex = [this](int i) -> std::string {
const char* DS = m_bUseLowerCase ? "0123456789abcdef" : "0123456789ABCDEF";
const char* DS = m_bSelectedOutputMode == OUTPUT_LHEX ? "0123456789abcdef" : "0123456789ABCDEF";
std::string result = "";

View file

@ -7,6 +7,7 @@
enum eOutputMode {
OUTPUT_CMYK = 0,
OUTPUT_HEX,
OUTPUT_LHEX,
OUTPUT_RGB,
OUTPUT_HSL,
OUTPUT_HSV
@ -44,8 +45,7 @@ class CHyprpicker {
bool m_bRenderInactive = false;
bool m_bNoZoom = false;
bool m_bNoFractional = false;
bool m_bDisableHexPreview = false;
bool m_bUseLowerCase = false;
bool m_bDisablePreview = false;
bool m_bRunning = true;

View file

@ -7,7 +7,7 @@
static void help(void) {
std::cout << "Hyprpicker usage: hyprpicker [arg [...]].\n\nArguments:\n"
<< " -a | --autocopy | Automatically copies the output to the clipboard (requires wl-clipboard)\n"
<< " -f | --format=fmt | Specifies the output format (cmyk, hex, rgb, hsl, hsv)\n"
<< " -f | --format=fmt | Specifies the output format (cmyk, lhex, hex, rgb, hsl, hsv)\n"
<< " -n | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n"
<< " -h | --help | Show this help message\n"
<< " -r | --render-inactive | Render (freeze) inactive displays\n"
@ -15,8 +15,7 @@ static void help(void) {
<< " -q | --quiet | Disable most logs (leaves errors)\n"
<< " -v | --verbose | Enable more logs\n"
<< " -t | --no-fractional | Disable fractional scaling support\n"
<< " -d | --disable-hex-preview | Disable live preview of Hex code\n"
<< " -l | --lowercase-hex | Outputs the hexcode in lowercase\n"
<< " -d | --disable-preview | Disable live preview of color\n"
<< " -V | --version | Print version info\n";
}
@ -34,12 +33,11 @@ int main(int argc, char** argv, char** envp) {
{"no-fractional", no_argument, NULL, 't'},
{"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'},
{"disable-hex-preview", no_argument, NULL, 'd'},
{"lowercase-hex", no_argument, NULL, 'l'},
{"disable-preview", no_argument, NULL, 'd'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}};
int c = getopt_long(argc, argv, ":f:hnarzqvtdlV", long_options, &option_index);
int c = getopt_long(argc, argv, ":f:hnarzqvtdV", long_options, &option_index);
if (c == -1)
break;
@ -49,6 +47,8 @@ int main(int argc, char** argv, char** envp) {
g_pHyprpicker->m_bSelectedOutputMode = OUTPUT_CMYK;
else if (strcasecmp(optarg, "hex") == 0)
g_pHyprpicker->m_bSelectedOutputMode = OUTPUT_HEX;
else if (strcasecmp(optarg, "lhex") == 0)
g_pHyprpicker->m_bSelectedOutputMode = OUTPUT_LHEX;
else if (strcasecmp(optarg, "rgb") == 0)
g_pHyprpicker->m_bSelectedOutputMode = OUTPUT_RGB;
else if (strcasecmp(optarg, "hsl") == 0)
@ -68,8 +68,7 @@ int main(int argc, char** argv, char** envp) {
case 't': g_pHyprpicker->m_bNoFractional = true; break;
case 'q': Debug::quiet = true; break;
case 'v': Debug::verbose = true; break;
case 'd': g_pHyprpicker->m_bDisableHexPreview = true; break;
case 'l': g_pHyprpicker->m_bUseLowerCase = true; break;
case 'd': g_pHyprpicker->m_bDisablePreview = true; break;
case 'V': {
std::cout << "hyprpicker v" << HYPRPICKER_VERSION << "\n";
exit(0);