Pixel-Composer/scripts/string_hexadecimal/string_hexadecimal.gml

52 lines
1.2 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
function string_hexadecimal(str){
2023-03-02 07:59:14 +01:00
static HEX = "0123456789ABCDEF";
2022-01-13 05:24:03 +01:00
var i = string_length(str);
var d = 1;
var v = 0;
while(i > 0) {
var ch = string_char_at(str, i);
2023-03-02 07:59:14 +01:00
var val = string_pos(string_upper(ch), HEX) - 1;
2022-01-13 05:24:03 +01:00
v += val * d;
d *= 16;
i--;
}
return v;
}
function number_to_hex(val) {
2023-02-28 09:43:01 +01:00
static HEX = "0123456789ABCDEF";
2022-01-13 05:24:03 +01:00
var ss = "";
while(val > 0) {
var i = val % 16;
ss = string_char_at(HEX, i + 1) + ss;
val = floor(val / 16);
}
while(string_length(ss) < 2) {
ss = "0" + ss;
}
return ss;
}
2023-02-28 09:43:01 +01:00
function color_get_hex(color, alpha = false) {
2023-03-31 06:59:08 +02:00
var arr = is_array(color) && array_length(color) == 4;
var r = arr? round(color[0] * 256) : color_get_red(color);
var g = arr? round(color[1] * 256) : color_get_green(color);
var b = arr? round(color[2] * 256) : color_get_blue(color);
var a = arr? round(color[3] * 256) : color_get_alpha(color);
2022-01-13 05:24:03 +01:00
2023-02-28 09:43:01 +01:00
var hex = number_to_hex(r) + number_to_hex(g) + number_to_hex(b) + (alpha? " " + number_to_hex(a) : "");
2022-01-13 05:24:03 +01:00
return hex;
2022-11-18 03:20:31 +01:00
}
function color_from_rgb(str) {
var _r = string_hexadecimal(string_copy(str, 1, 2));
var _g = string_hexadecimal(string_copy(str, 3, 2));
var _b = string_hexadecimal(string_copy(str, 5, 2));
return make_color_rgb(_r, _g, _b);
2022-01-13 05:24:03 +01:00
}