Colour Code from JSON/INI

Discussion in 'Tutorials' started by KH-SOS, Sep 1, 2016.

  1. KH-SOS

    KH-SOS Guest

    GM Version: 1.4.1757
    Target Platform: ALL
    Download: N/A
    Links: N/A

    Getting a colour code from a single stored value

    So here's a perfectly painful solution to the perfectly normal desire to get a colour code from saved text like a JSON file. Unfortunately GM:S doesn't let you just drop in a hexcode for colour (#000000 for black, #FF00FF for hot pink, etc) like everybody else in the world uses.

    The make_colour_rgb or make_colour_hsv functions require you to give separate variables for each value. If you want to save a single string for your colour code and you're using standard digits each code can be 1-255, unfortunately different string lengths to parse out and re-enter for the three values. I haven't had much luck getting hex numbers to work in GMS, and eventually I got tired of padding my numbers to be three digits ("1" as "001").

    So until GM:S supports just dropping in hexcodes for colours like everybody else in the world, here's a crazy overcomplicated solution for saving colour codes as a single string and dynamically handling the character length of the digits: I'll be happy if it saves anyone the hassle of figuring out such a basic desire:

    var mapbgcode= ds_map_find_value(yourdatamap, "mapbgcolour");
    //1-3 digit rgb
    var firstbreak=string_pos(",",mapbgcode);
    var stillremaining=string_copy(mapbgcode,firstbreak+1,string_length(mapbgcode));
    var secondbreak=string_pos(",",stillremaining);
    var mapred=(string_copy(mapbgcode,1,firstbreak-1));
    var mapgreen=(string_copy(mapbgcode,firstbreak+1,secondbreak-1));
    var convoluted=string_length(mapred)+1+string_length(mapgreen)+1+1;
    var mapblue=(string_copy(mapbgcode,convoluted,string_length(mapbgcode)-convoluted));
    "yourdatamap" is your decoded json,
    "mapbgcolour" is the key for the saved colour value (a string of a #,#,# - where each number can be 0 to 255)
    and the name of the variable with the final colour code.
    Good grief.
    Turgon likes this.
  2. chance

    chance predictably random Forum Staff Moderator

    Apr 22, 2016
    Not sure what you mean. GML accepts hex colors with the "$" prefix. But the order is blue-green-red. Maybe that's what you mean by not accepting conventional hexcodes.

    Either way, your script to parse comma-separated triplets could be useful for someone. It's simple, but it seems to work correctly (as long as the input format is correct).
  3. KH-SOS

    KH-SOS Guest

    So you enter the rgb code by giving blue then green then red?!?!?

    make_colour_rgb=$FF8000 (to get orange) really should be a thing. Who on earth figures an RGB number should be blue value first???
  4. Mercerenies

    Mercerenies Member

    Jun 21, 2016
    Microsoft. GM conforms to Microsoft's way of doing things because it used to be a Windows-only export software. Yes, it's absurd, but it's not actually YYG's fault.

    Also, if you're storing color codes in JSON, you should be using lists, so you can just list the RGB values out in a JSON list. That argument doesn't apply for INI, which is where your script would be most useful.
    Turgon and Aura like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice