In my game I can import a color correctly when it is in the "engine format".

Example ("color.ini"):


These colors were seen in this link:


It turns out that I can't import the color when I use its hexadecimal code (only the colors from the previous link don't supply me).

Example ("color.ini"):


Codes taken from this link:


  • I tried lowercase variations, with $ instead of # and nothing worked.

  • It is not even a case of seeing the wrong color, but of changing nothing (it always turns white).

  • I am using the function (asset_get_index) to apply the color.

Link where I looked for a solution before coming here:


Code in my game (works in all colors that don't come as hexadecimal):




Code I tried, based on one of the comments, from the question:


for(var i=1;i<=string_length(color1);i++){

    if (c>=zero && c<=nine){
    }else if(c>=A && c<=f){

I used the function (show_debug_message) to see the result of the code and got nonsense results:

  • 1024, 2048

Link to see the code base I tried:


  • \$\begingroup\$ Did you try parsing your hexadecimal code string to a number first, before assigning it to your color array, as described here? \$\endgroup\$
    – DMGregory
    Aug 22, 2020 at 12:33
  • \$\begingroup\$ @DMGregory I looked at what you sent, but I still haven't been able to apply it. \$\endgroup\$ Aug 26, 2020 at 0:01
  • \$\begingroup\$ What did you try? \$\endgroup\$
    – DMGregory
    Aug 26, 2020 at 0:40
  • \$\begingroup\$ I copied and adapted the code. The results were numbers. \$\endgroup\$ Aug 26, 2020 at 23:15
  • 1
    \$\begingroup\$ It already was a hexadecimal string — that was the problem. Now you've converted it to a number that the engine can understand natively. Just because it prints as 1024 to display to you, the human, doesn't mean its bits are anything other than 0000 0000 0000 0000 0000 0100 0000 0000 in the computer's memory. \$\endgroup\$
    – DMGregory
    Aug 31, 2020 at 17:06

1 Answer 1


I read this article to jog my memory about HEX colour indexing, which is basically a base-16 that can be converted easily to decimal values, used in GMS to create colour values with the make_colour_rgb(r, g, b) function.

The paragraph "Converting Hex to RGB Color Codes" was my main guide through the implementation, as it explains crystal clear how to convert values from hexadecimal to decimal values, grouping them for red, green, and blue values:

  • Take the first digit, D, and convert it into decimal. The result is 13.
  • Multiply 13 by 16. 13 (16) = 208. This is the partial value.
  • Take the second digit, A, and convert it into decimal. The result is 10.
  • Add the partial value to the second digit. 208 + 10 = 218.

And so on for all the remaining values. Then I came up with quite a simple implementation:

// Converts a HEX colour value in an RGB map
// INPUT:   string
// OUTPUT:  ds_map["red", "green", "blue"] OR noone
var hex_code = string_upper(string_lettersdigits(argument0));
var rgb_map = ds_map_create();
ds_map_add( rgb_map, "red",
    16 * hex_to_dec(string_char_at(hex_code, 1)) +
    hex_to_dec(string_char_at(hex_code, 2)) );
ds_map_add( rgb_map, "green",
    16 * hex_to_dec(string_char_at(hex_code, 3)) +
    hex_to_dec(string_char_at(hex_code, 4)) );
ds_map_add( rgb_map, "blue",
    16 * hex_to_dec(string_char_at(hex_code, 5)) +
    hex_to_dec(string_char_at(hex_code, 6)) );
return rgb_map;

This function takes a string in the format #000000 (including the hash character) and converts it to RGB values stored in a ds_map, and accepts both uppercase and lowercase letters.

The support function hex_to_dec() is used to convert individual HEX values into their decimal counterparts by checking against their ASCII values:

// Converts a HEX value in a decimal number
// INPUT:   string
// OUTPUT:  real
if ( ord(argument0) >= 48 && ord(argument0) <= 57 ) return real(argument0);
if ( ord(argument0) >= 65 && ord(argument0) <= 90 ) return ((ord(argument0) - 65) mod 6) + 10;

This function takes a 1-char string and returns its value in the decimal system, it does the same for letters A-F and loops back (thanks to mod) for other letters (which is a nicer alternative to throwing an exception, in my opinion, despite harder debugging).

There's so much to do to improve this code, but it works fine as a starting point. It's up to you to deal with wrong or missing input, according to your needs.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .