Question - Code Why are hex colours #BBGGRR instead of #RRGGBB?

Discussion in 'GameMaker Studio 2 Community Tech Support' started by Big_Macca_101, Jan 4, 2017.

  1. Big_Macca_101

    Big_Macca_101 Member

    Joined:
    Jun 20, 2016
    Posts:
    114
    Just curious as to why GMS uses hexadecimal colours in the format of Blue/Green/Red rather than Red/Green/Blue like most other programs and languages use?

    Its just a bit confusing to me and to other's I would imagine.
     
  2. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    170
    Because colors are stored in little endian. draw_set_color() takes only one argument, which is color. You'll notice that make_color_rgb() is in the standard format, and this outputs a little endian color value.

    As far as what "little endian" means, that's a complicated subject and a totally different question. If you want to learn a bit more, this is a good place to start.
     
    Big_Macca_101 likes this.
  3. Big_Macca_101

    Big_Macca_101 Member

    Joined:
    Jun 20, 2016
    Posts:
    114
    Awesome thankyou, I'll look into this, might just write a script to convert an RGB hex format to GMS colour.
     
  4. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,226
    It's a leftover from Delphi, which used "little endian" format.
     
  5. Juju

    Juju Member

    Joined:
    Jun 20, 2016
    Posts:
    376
    BGR is used elsewhere as well, though I forget where off hand.
     
  6. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    170
    EDIT: Use gnysek's script below instead.

    I was bored and also wanted to write my colors as RGB so I just wrote up this simple RGB to BGR script. Feel free to use it.

    Code:
    ///convert_color_endianness(val)
    //Converts a color from RGB to the little endian BGR and vice versa
    var rgb = argument[0],
        bgr = 0,   //Output
        bsh = $10; //Number of bits to shift
    
    bgr = (rgb & $FF) << bsh;      //Blue
    bgr += (rgb & $FF00);          //Green
    bgr += (rgb & $FF0000) >> bsh; //Red
    
    return bgr;
    Also feel free to change the name. It's pretty long and I didn't put much thought into it.
     
    Last edited: Jan 4, 2017
    csanyk likes this.
  7. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,226

    Code:
    /// @desc rgb_to_bgr - converts RGB int/hex to BGR real
    /// @param rgb_color {real} example: $00BBFF or 48127
    
    return (argument0 & $FF) << 16 | (argument0 & $FF00) | (argument0 & $FF0000) >> 16;
    I love one liners <3 !
     
    nacho_chicken likes this.
  8. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    170
    I knew I was forgetting something. I even normally do that with 16-bit ints. It's too late for me to code, haha.
     
  9. Storyteller

    Storyteller Member

    Joined:
    Jul 19, 2016
    Posts:
    237
    there really seems to be no reason to keep things this way. being compatible with other languages, libraries and standards of practice is a good thing. all the oddities that remain for backwards compatibility are part of what keeps GM from reaching its potential. there is no good reason to keep this non-standard practice.
     

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