Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 3- and 4-digit hex colors #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,33 @@ static uint32_t parse_color(const char *color) {
++color;
}

int len = strlen(color);
if (len != 6 && len != 8) {
swaylock_log(LOG_DEBUG, "Invalid color %s, defaulting to 0xFFFFFFFF",
color);
return 0xFFFFFFFF;
}
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
if (strlen(color) == 6) {
res = (res << 8) | 0xFF;
size_t len = strlen(color);
if (len == 3 || len == 4) {
errno = 0;
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
if (errno == 0) {
if (len == 3) {
res = res << 4 | 0xF;
}
uint32_t r = ((res >> 12) & 0xF) * 0x11000000;
uint32_t g = ((res >> 8) & 0xF) * 0x00110000;
uint32_t b = ((res >> 4) & 0xF) * 0x00001100;
uint32_t a = (res & 0xF) * 0x00000011;
return r | g | b | a;
}
} else if (len == 6 || len == 8) {
errno = 0;
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
if (errno == 0) {
if (len == 6) {
res = res << 8 | 0xFF;
}
return res;
}
}
return res;

swaylock_log(LOG_DEBUG, "Invalid color %s, defaulting to 0xFFFFFFFF", color);
return 0xFFFFFFFF;
}

int lenient_strcmp(char *a, char *b) {
Expand Down