You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
883 B
Plaintext
43 lines
883 B
Plaintext
package main
|
|
|
|
import "core:encoding/hex"
|
|
import "core:log"
|
|
import "core:strings"
|
|
|
|
hex_to_rgb :: proc(hex_str: string) -> (red, blue, green: u8, ok: bool) {
|
|
builder, err := strings.builder_make()
|
|
if err != nil {
|
|
log.error("failed to initialize memory")
|
|
return 0, 0, 0, false
|
|
}
|
|
|
|
rgb_values: [3]u8
|
|
rgb_count := 0
|
|
for char, char_idx in hex_str {
|
|
if char == '#' {
|
|
continue
|
|
}
|
|
if rgb_count > 2 {
|
|
log.error("Malformed hex colour")
|
|
return 0, 0, 0, false
|
|
}
|
|
strings.write_rune(&builder, char)
|
|
if strings.builder_len(builder) == 2 {
|
|
str := strings.to_string(builder)
|
|
rgb_value, ok := hex.decode_sequence(str)
|
|
if !ok {
|
|
log.error("failed to initialize memory")
|
|
return 0, 0, 0, false
|
|
}
|
|
strings.builder_reset(&builder)
|
|
|
|
rgb_values[rgb_count] = rgb_value
|
|
|
|
rgb_count += 1
|
|
}
|
|
}
|
|
|
|
return rgb_values[0], rgb_values[1], rgb_values[2], true
|
|
}
|
|
|