feat: adds flag for css color function output

This commit is contained in:
Joel Eapen 2025-10-03 20:32:58 +05:30
parent b3f3f230c9
commit 75e6522d31
No known key found for this signature in database
GPG key ID: F340168A600AAAE3
4 changed files with 75 additions and 22 deletions

View file

@ -51,6 +51,8 @@ The available options are:
.Pp
The default format is
.Ar hex .
.It Fl c , Fl Fl css
Output the color in CSS color function syntax
.It Fl n , Fl Fl no-fancy
Disable colored output.
Default behavior is to color the output in the same color as the selected pixel.
@ -73,7 +75,7 @@ Get a pixels color in HSL, wrapped in a CSS
.Fn hsl
function:
.Pp
.Dl $ hyprpicker -f hsl | sed 's/^/hsl(/; s/$/)/; y/ /,/'
.Dl $ hyprpicker -cf hsl
.Sh SEE ALSO
.Xr hyprctl 1 ,
.Xr hyprland 1 ,

View file

@ -672,6 +672,7 @@ void CHyprpicker::initMouse() {
return result;
};
std::string formattedColor;
std::string hexColor = std::format("#{0:02x}{1:02x}{2:02x}", COL.r, COL.g, COL.b);
switch (m_bSelectedOutputMode) {
@ -679,12 +680,22 @@ void CHyprpicker::initMouse() {
float c, m, y, k;
COL.getCMYK(c, m, y, k);
std::string formattedColor = std::format("{}% {}% {}% {}%", c, m, y, k);
if (m_bFancyOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g%% %g%% %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, c, m, y, k);
if (m_bCssOutput)
formattedColor = std::format("cmyk({}%, {}%, {}%, {}%)", c, m, y, k);
else
Debug::log(NONE, "%g%% %g%% %g%% %g%%", c, m, y, k);
formattedColor = std::format("{}% {}% {}% {}%", c, m, y, k);
if (m_bFancyOutput) {
if (m_bCssOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%imcmyk(%g%%, %g%%, %g%%, %g%%)\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, c, m, y, k);
else
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g%% %g%% %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, c, m, y, k);
} else {
if (m_bCssOutput)
Debug::log(NONE, "cmyk(%g%%, %g%%, %g%%, %g%%)", c, m, y, k);
else
Debug::log(NONE, "%g%% %g%% %g%% %g%%", c, m, y, k);
}
if (m_bAutoCopy)
NClipboard::copy(formattedColor);
@ -712,12 +723,22 @@ void CHyprpicker::initMouse() {
break;
}
case OUTPUT_RGB: {
std::string formattedColor = std::format("{} {} {}", COL.r, COL.g, COL.b);
if (m_bFancyOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%i %i %i\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, COL.r, COL.g, COL.b);
if (m_bCssOutput)
formattedColor = std::format("rgb({}, {}, {})", COL.r, COL.g, COL.b);
else
Debug::log(NONE, "%i %i %i", COL.r, COL.g, COL.b);
formattedColor = std::format("{} {} {}", COL.r, COL.g, COL.b);
if (m_bFancyOutput) {
if (m_bCssOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%imrgb(%i, %i, %i)\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, COL.r, COL.g, COL.b);
else
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%i %i %i\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, COL.r, COL.g, COL.b);
} else {
if (m_bCssOutput)
Debug::log(NONE, "rgb(%i, %i, %i)", COL.r, COL.g, COL.b);
else
Debug::log(NONE, "%i %i %i", COL.r, COL.g, COL.b);
}
if (m_bAutoCopy)
NClipboard::copy(formattedColor);
@ -729,19 +750,45 @@ void CHyprpicker::initMouse() {
break;
}
case OUTPUT_HSL:
float h, s, l;
COL.getHSL(h, s, l);
if (m_bCssOutput)
formattedColor = std::format("hsl({}, {}%, {}%)", h, s, l);
else
formattedColor = std::format("{} {}% {}%", h, s, l);
if (m_bFancyOutput) {
if (m_bCssOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%imhsl(%g, %g%%, %g%%)\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, h, s, l);
else
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, h, s, l);
} else {
if (m_bCssOutput)
Debug::log(NONE, "hsl(%g, %g%%, %g%%)", h, s, l);
else
Debug::log(NONE, "%g %g%% %g%%", h, s, l);
}
break;
case OUTPUT_HSV: {
float h, s, l_or_v;
if (m_bSelectedOutputMode == OUTPUT_HSV)
COL.getHSV(h, s, l_or_v);
float h, s, v;
COL.getHSV(h, s, v);
if (m_bCssOutput)
formattedColor = std::format("hsv({}, {}%, {}%)", h, s, v);
else
COL.getHSL(h, s, l_or_v);
formattedColor = std::format("{} {}% {}%", h, s, v);
std::string formattedColor = std::format("{} {}% {}%", h, s, l_or_v);
if (m_bFancyOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, h, s, l_or_v);
else
Debug::log(NONE, "%g %g%% %g%%", h, s, l_or_v);
if (m_bFancyOutput) {
if (m_bCssOutput)
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%imhsv(%g, %g%%, %g%%)\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, h, s, v);
else
Debug::log(NONE, "\033[38;2;%i;%i;%i;48;2;%i;%i;%im%g %g%% %g%%\033[0m", FG, FG, FG, COL.r, COL.g, COL.b, h, s, v);
} else {
if (m_bCssOutput)
Debug::log(NONE, "hsv(%g, %g%%, %g%%)", h, s, v);
else
Debug::log(NONE, "%g %g%% %g%%", h, s, v);
}
if (m_bAutoCopy)
NClipboard::copy(formattedColor);

View file

@ -39,6 +39,7 @@ class CHyprpicker {
eOutputMode m_bSelectedOutputMode = OUTPUT_HEX;
bool m_bFancyOutput = true;
bool m_bCssOutput = false;
bool m_bAutoCopy = false;
bool m_bNotify = false;

View file

@ -8,6 +8,7 @@ static void help() {
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"
<< " -c | --css | Output the color in CSS color function syntax\n"
<< " -n | --notify | Sends a desktop notification when a color is picked (requires notify-send and a notification daemon like dunst)\n"
<< " -b | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n"
<< " -h | --help | Show this help message\n"
@ -29,6 +30,7 @@ int main(int argc, char** argv, char** envp) {
while (true) {
int option_index = 0;
static struct option long_options[] = {{"autocopy", no_argument, nullptr, 'a'},
{"css", required_argument, nullptr, 'c'},
{"format", required_argument, nullptr, 'f'},
{"help", no_argument, nullptr, 'h'},
{"no-fancy", no_argument, nullptr, 'b'},
@ -45,7 +47,7 @@ int main(int argc, char** argv, char** envp) {
{"radius", required_argument, nullptr, 'u'},
{nullptr, 0, nullptr, 0}};
int c = getopt_long(argc, argv, ":f:hnbarzqvtdlVs:u:", long_options, &option_index);
int c = getopt_long(argc, argv, ":f:hnbacrzqvtdlVs:u:", long_options, &option_index);
if (c == -1)
break;
@ -68,6 +70,7 @@ int main(int argc, char** argv, char** envp) {
break;
case 'h': help(); exit(0);
case 'b': g_pHyprpicker->m_bFancyOutput = false; break;
case 'c': g_pHyprpicker->m_bCssOutput = true; break;
case 'n': g_pHyprpicker->m_bNotify = true; break;
case 'a': g_pHyprpicker->m_bAutoCopy = true; break;
case 'r': g_pHyprpicker->m_bRenderInactive = true; break;