diff options
Diffstat (limited to 'src/user/highway')
-rw-r--r-- | src/user/highway/line.c | 31 | ||||
-rw-r--r-- | src/user/highway/main.c | 1 | ||||
-rw-r--r-- | src/user/highway/vars.c | 44 |
3 files changed, 62 insertions, 14 deletions
diff --git a/src/user/highway/line.c b/src/user/highway/line.c index 5d1e543..3bc3bf2 100644 --- a/src/user/highway/line.c +++ b/src/user/highway/line.c @@ -12,18 +12,33 @@ #define LINE_SIZE 4096 static char line[LINE_SIZE]; -static inline uint8_t hex_to_int(char ch) { +static inline uint8_t hex_digit_to_int(char ch) { return ch - (ch <= '9' ? '0' : 'a' - 10); } +static inline uint8_t hex_byte_to_int(const char *ch) { + return (hex_digit_to_int(ch[0]) << 4) | hex_digit_to_int(ch[1]); +} + +static inline _pixel_t hex_to_pixel(const char *ch) { + return (_pixel_t){ + .r = hex_byte_to_int(ch), + .g = hex_byte_to_int(ch + 2), + .b = hex_byte_to_int(ch + 4) + }; +} + void ensure_color() { - const struct no_null_sn *color = get_var((struct no_null_sn){.data = "_color", .length = 6}); - //fgbg - if (color) - term_set_color( - (hex_to_int(color->data[0]) << 4) | hex_to_int(color->data[1]), - (hex_to_int(color->data[2]) << 4) | hex_to_int(color->data[3]) - ); + const struct no_null_sn *color; +get_color: + color = get_var((struct no_null_sn){.data = "_color", .length = 6}); + if (color && (color->length == 12)) + //rgb, fgbg + term_set_color(hex_to_pixel(color->data), hex_to_pixel(color->data + 6)); + else { + new_color(); + goto get_color; + } } static void line_replace(const char *from) { diff --git a/src/user/highway/main.c b/src/user/highway/main.c index 59586c1..47f3985 100644 --- a/src/user/highway/main.c +++ b/src/user/highway/main.c @@ -8,7 +8,6 @@ #include "vars.h" void main(const char *arg) { - new_color(); source(*arg ? arg : "user/default.rc"); ensure_color(); diff --git a/src/user/highway/vars.c b/src/user/highway/vars.c index ed9fa3a..2d53c3a 100644 --- a/src/user/highway/vars.c +++ b/src/user/highway/vars.c @@ -84,7 +84,7 @@ void dump_vars() { } static const char hex_digits[] = "0123456789abcdef"; -static char color[] = {'1', '0', '.', '.'}; +static char color[12] = "000000......"; static const struct no_null_sn color_name = { .data = "_color", @@ -93,12 +93,46 @@ static const struct no_null_sn color_name = { static const struct no_null_sn color_value = { .data = color, - .length = 4 + .length = 12 }; void new_color() { - const uint8_t bg = gen_rand() % 0x30 + 0x38; - color[2] = hex_digits[bg >> 4]; - color[3] = hex_digits[bg & 0xf]; + const uint16_t hue = gen_rand() % (0x33 * 6); + _pixel_t bg = {.r = 0xff, .g = 0xcc, .b = 0xcc}; + if (hue <= 0x33) { + bg.b += hue; + goto got_bg; + } + bg.b += 0x33; + if (hue <= 0x33 * 2) { + bg.r -= hue - 0x33; + goto got_bg; + } + bg.r -= 0x33; + if (hue <= 0x33 * 3) { + bg.g += hue - 0x33 * 2; + goto got_bg; + } + bg.g += 0x33; + if (hue <= 0x33 * 4) { + bg.b -= hue - 0x33 * 3; + goto got_bg; + } + bg.b -= 0x33; + if (hue <= 0x33 * 5) { + bg.r += hue - 0x33 * 4; + goto got_bg; + } + bg.r += 0x33; + bg.g -= hue - 0x33 * 5; + +got_bg: + color[ 6] = hex_digits[bg.r / 16]; + color[ 7] = hex_digits[bg.r % 16]; + color[ 8] = hex_digits[bg.g / 16]; + color[ 9] = hex_digits[bg.g % 16]; + color[10] = hex_digits[bg.b / 16]; + color[11] = hex_digits[bg.b % 16]; + set_var(color_name, color_value); }
\ No newline at end of file |