Skip to content

Commit

Permalink
feat: adds flag to disable live preview of hex code
Browse files Browse the repository at this point in the history
  • Loading branch information
bun137 committed Nov 25, 2024
1 parent 2fa6623 commit 5e60050
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 59 deletions.
91 changes: 46 additions & 45 deletions src/hyprpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,57 +424,58 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) {
cairo_arc(PCAIRO, CLICKPOS.x, CLICKPOS.y, 100 / SCALEBUFS.x, 0, 2 * M_PI);
cairo_clip(PCAIRO);
cairo_paint(PCAIRO);
if (!m_bDisableLive) {
const auto currentColor = getColorFromPixel(pSurface, CLICKPOS);
std::string hexBuffer = std::format("#{:02X}{:02X}{:02X}", currentColor.r, currentColor.g, currentColor.b);

cairo_set_source_rgba(PCAIRO, 0.0, 0.0, 0.0, 0.5);

double x, y, width = 85, height = 28, radius = 6;

if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
y = CLICKPOS.y - 40;
} else if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50)) {
x = CLICKPOS.x;
y = CLICKPOS.y - 40;
} else if (CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
y = CLICKPOS.y + 20;
} else {
x = CLICKPOS.x;
y = CLICKPOS.y + 20;
}

const auto currentColor = getColorFromPixel(pSurface, CLICKPOS);
std::string hexBuffer = std::format("#{:02X}{:02X}{:02X}", currentColor.r, currentColor.g, currentColor.b);

cairo_set_source_rgba(PCAIRO, 0.0, 0.0, 0.0, 0.5);

double x, y, width = 85, height = 28, radius = 6;

if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
y = CLICKPOS.y - 40;
} else if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50)) {
x = CLICKPOS.x;
y = CLICKPOS.y - 40;
} else if (CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
y = CLICKPOS.y + 20;
} else {
x = CLICKPOS.x;
y = CLICKPOS.y + 20;
}
cairo_move_to(PCAIRO, x + radius, y);
cairo_arc(PCAIRO, x + width - radius, y + radius, radius, -M_PI_2, 0);
cairo_arc(PCAIRO, x + width - radius, y + height - radius, radius, 0, M_PI_2);
cairo_arc(PCAIRO, x + radius, y + height - radius, radius, M_PI_2, M_PI);
cairo_arc(PCAIRO, x + radius, y + radius, radius, M_PI, -M_PI_2);

cairo_move_to(PCAIRO, x + radius, y);
cairo_arc(PCAIRO, x + width - radius, y + radius, radius, -M_PI_2, 0);
cairo_arc(PCAIRO, x + width - radius, y + height - radius, radius, 0, M_PI_2);
cairo_arc(PCAIRO, x + radius, y + height - radius, radius, M_PI_2, M_PI);
cairo_arc(PCAIRO, x + radius, y + radius, radius, M_PI, -M_PI_2);
cairo_close_path(PCAIRO);
cairo_fill(PCAIRO);

cairo_close_path(PCAIRO);
cairo_fill(PCAIRO);
cairo_set_source_rgba(PCAIRO, 1.0, 1.0, 1.0, 1.0);
cairo_select_font_face(PCAIRO, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(PCAIRO, 18);

cairo_set_source_rgba(PCAIRO, 1.0, 1.0, 1.0, 1.0);
cairo_select_font_face(PCAIRO, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(PCAIRO, 18);

double padding = 5.0;
double textX = x + padding;

if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
cairo_move_to(PCAIRO, textX, CLICKPOS.y - 20);
} else if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50)) {
cairo_move_to(PCAIRO, textX, CLICKPOS.y - 20);
} else if (CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
} else {
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
}
double padding = 5.0;
double textX = x + padding;

cairo_show_text(PCAIRO, hexBuffer.c_str());
if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
cairo_move_to(PCAIRO, textX, CLICKPOS.y - 20);
} else if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50)) {
cairo_move_to(PCAIRO, textX, CLICKPOS.y - 20);
} else if (CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
} else {
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
}

cairo_surface_flush(PBUFFER->surface);
cairo_show_text(PCAIRO, hexBuffer.c_str());

cairo_surface_flush(PBUFFER->surface);
}
cairo_restore(PCAIRO);
cairo_pattern_destroy(PATTERN);
}
Expand Down
3 changes: 2 additions & 1 deletion src/hyprpicker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CHyprpicker {
bool m_bRenderInactive = false;
bool m_bNoZoom = false;
bool m_bNoFractional = false;
bool m_bDisableLive = false;

bool m_bRunning = true;

Expand Down Expand Up @@ -76,4 +77,4 @@ class CHyprpicker {
private:
};

inline std::unique_ptr<CHyprpicker> g_pHyprpicker;
inline std::unique_ptr<CHyprpicker> g_pHyprpicker;
21 changes: 8 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,21 @@ 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"
<< " -V | --version | Print version info\n";
<< " -V | --version | Print version info\n"
<< " -d | --disable-live | Disable live preview of Hex code\n";
}

int main(int argc, char** argv, char** envp) {
g_pHyprpicker = std::make_unique<CHyprpicker>();

while (true) {
int option_index = 0;
static struct option long_options[] = {{"autocopy", no_argument, NULL, 'a'},
{"format", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"no-fancy", no_argument, NULL, 'n'},
{"render-inactive", no_argument, NULL, 'r'},
{"no-zoom", no_argument, NULL, 'z'},
{"no-fractional", no_argument, NULL, 't'},
{"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}};
static struct option long_options[] = {{"autocopy", no_argument, NULL, 'a'}, {"format", required_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'},
{"no-fancy", no_argument, NULL, 'n'}, {"render-inactive", no_argument, NULL, 'r'}, {"no-zoom", no_argument, NULL, 'z'},
{"no-fractional", no_argument, NULL, 't'}, {"quiet", no_argument, NULL, 'q'}, {"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'}, {"disable-live", no_argument, NULL, 'd'}, {NULL, 0, NULL, 0}};

int c = getopt_long(argc, argv, ":f:hnarzqvtV", long_options, &option_index);
int c = getopt_long(argc, argv, ":f:hnarzqvtVd", long_options, &option_index);
if (c == -1)
break;

Expand Down Expand Up @@ -64,6 +58,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_bDisableLive = true; break;
case 'V': {
std::cout << "hyprpicker v" << HYPRPICKER_VERSION << "\n";
exit(0);
Expand Down

0 comments on commit 5e60050

Please sign in to comment.