GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2023-11-24 09:56:40

zelenqk
Member
Registered: 2023-11-24
Posts: 2

draw_string_colored

with this script you can draw strings with multiple colors!
color codes can be rgb hsv or just plain alpha but rgb and hsv can have 4 arguments the fourth one being the alpha so
you can write rgb like this

<rgb = 233, 32, 35> without alpha
<rgb = 233, 32, 35, 0.5> with alpha

hsv like this
<hsv = 233, 32, 35> without alpha
<hsv = 233, 32, 35, 0.5> with alpha

and alpha like this
<a = 0.4> alpha is an integer that can be anything between 0 and 1

here is an example string with these color codes

Expandvar my_string = "<rgb = 0, 0, 255> im blu da ba de <rgb = 0, 255, 0> im green da ba de"
draw_string_colored(0, 0, my_string);

remember to use the script inside a draw event!

and here is the code with a total of 3 scripts! cool

Expandfunction get_hsv(code){
	code = string_split(code, ",", true);
	
	var color = (array_length(code) == 3) ? make_color_hsv(real(code[0]), real(code[1]), real(code[2])) : (array_length(code) == 4 ? [make_color_hsv(real(code[0]), real(code[1]), real(code[2])), real(code[3])] : [c_white, 1])
	
	return color;
}

function get_rgb(code){
	code = string_split(code, ",", true);
	
	var color = (array_length(code) == 3) ? make_color_rgb(real(code[0]), real(code[1]), real(code[2])) : (array_length(code) == 4 ? [make_color_rgb(real(code[0]), real(code[1]), real(code[2])), real(code[3])] : [c_white, 1])
	
	return color;
}

function draw_string_colored(x_, y_, text){
	var color, alpha, codes, whole_string;
	
	color = c_white;
	alpha = 1;
	codes = string_split(text, "<", true);
	whole_string = "";
	
	for(var i = 0; i < array_length(codes); i++){
		var code = string_split(codes[i], ">", true);
		alpha = 1;
		
		if (array_length(code) > 1){
			var current_text = code[1];
			code = string_replace_all(code[0], " ", "");
			
			var type = string_split(code, "=", true);
			code = type[1];
			type = type[0];
			
			switch (type){
			case "rgb":
				color = get_rgb(code);
				
				if is_array(color){
					alpha = color[1];
					color = color[0];
				}
				break;
			case "a":
				alpha = real(code);
				break;
			case "hsv":
				color = get_hsv(code);
				
				if is_array(color){
					alpha = color[1];
					color = color[0];
				}
				break;
			}
		}else{
			current_text = 	text;
		}
		
		draw_text_color(x_ + string_width(whole_string), y_, current_text, color, color, color, color, alpha);
			
		whole_string += current_text;
	}
	
	return whole_string;
}

edit///
returns the string you inputed but without any color codes

Last edited by zelenqk (2023-11-24 10:06:34)

Offline

#2 2023-11-24 09:57:40

zelenqk
Member
Registered: 2023-11-24
Posts: 2

Re: draw_string_colored

just paste it all inside a script and youre ready cool

Offline

Board footer

Powered by FluxBB