locked
Converting hexidecimal colour code to RGB. RRS feed

  • General discussion

  • Hello guys,

    First post on this forum, I have been using SB for a while now for some personal projects to keep me busy. Recently I thought it would be nice to have a manner of converting Hexi-RGB as the RGB-Hexi feature is already available. I wrote a code for firstly converting the Hexidecimal to RGB and then converting the RGB values into a luminosity value for the gray scale code. Once the luminosity value was achieved I simply replaced R, G & B with the luminosity value. (We all know that three identical values for R,G,B makes a grayscale). I used a certain fractile of each of the colours to calculate the luminosity I found from some papers and of course wiki. Lastly I converted the RGB back to Hexidecimal for the sake of dragging the code out further. :)

    What do you guys think? I applied it to an array code for recolouring images, it was somewhat slow pixel by pixel... But it was the challenge I was working towards.

    Code Below:

     

    TextWindow.WriteLine(" ")
    TextWindow.Write(" Please type the Hexidecimal code (excluding the '#' at the start): ")
    hexi = TextWindow.Read()
    '____________________Converting Hexidecimal to RGB___________'
    hexchar1 = Text.GetSubText(hexi,1,1)
    hexchar2 = Text.GetSubText(hexi,2,1)
    hexchag1 = Text.GetSubText(hexi,3,1)
    hexchag2 = Text.GetSubText(hexi,4,1)
    hexchab1 = Text.GetSubText(hexi,5,1)
    hexchab2 = Text.GetSubText(hexi,6,1)
    hcol[1] = 1
    hcol[2] = 2
    hcol[3] = 3
    hcol[4] = 4
    hcol[5] = 5
    hcol[6] = 6
    hcol[7] = 7
    hcol[8] = 8
    hcol[9] = 9
    hcol["A"] = 10
    hcol["B"] = 11
    hcol["C"] = 12
    hcol["D"] = 13
    hcol["E"] = 14
    hcol["F"] = 15
    rval = (hcol[hexchar1]*16)+hcol[hexchar2]
    gval = (hcol[hexchag1]*16)+hcol[hexchag2]
    bval = (hcol[hexchab1]*16)+hcol[hexchab2]
    '_______________CONVERTING_RGB_TO_LUMINANCE______'
    lumin = Math.SquareRoot(((rval*rval)*0.3)+((gval*gval)*0.59)+((bval*bval)*0.11))
    lumr = Math.Round(lumin)
    greyhex = GraphicsWindow.GetColorFromRGB(lumr,lumr,lumr)
    TextWindow.WriteLine(" ")
    TextWindow.WriteLine(" "+greyhex)

     


    Alex Warrington - alex.warrington@live.co.uk
    • Edited by Alexwarrington Friday, April 15, 2011 8:27 PM Incorrectly stated the code.
    Thursday, April 14, 2011 9:32 PM

All replies

  • This is pretty cool. But if you wanted to use grayscale hexidecimal in your code (like setting the brushcolor), then it won't work. This is because SB only accepts colors as string or RGB. But it would be really cool if you could create a hexidecimal to RGB program.
    ~~AirWaves!!~~
    Friday, April 15, 2011 2:13 AM
  • Yes i understand what you mean about the hex-grayscale. Its not hexi at all i was wrong it just gets the colour from RGB.

     

     "But it would be really cool if you could create a hexidecimal to RGB program."

    I believe everything before '_______________CONVERTING_RGB_TO_LUMINANCE______'

    In the code is a simple conversion of hexidecimal to RGB.

    JPR113

    I have uploaded the HEXIDECIMAL to RGB coverter.

    Excuse the lack of design.

    Better?


    Alex Warrington - alex.warrington@live.co.uk
    Friday, April 15, 2011 10:12 AM
  • And attaching the previous program.

    This program returns a Hexidecimal code that has been grayscaled from the original code.

    LMV451

    Please feel free to use and take codes. Leave feedback :)


    Alex Warrington - alex.warrington@live.co.uk
    Friday, April 15, 2011 10:19 AM
  • Oh, sorry for overlooking your code. I must have not read it thoroughly enough. If you wanted to use this in another program, say a game or a painting program, then this code wouldn't be helpful, because most painting programs use the GraphicsWindow. I could make this into an extension for you, so you could just use one function to do all that code, and so you can just use parameters instead of the TextWindow. Would that be alright?
    ~~AirWaves!!~~
    Friday, April 15, 2011 4:11 PM
  • Id love it to be mad into an extension. Thanks for your lookover. 
    Alex Warrington - alex.warrington@live.co.uk
    Friday, April 15, 2011 6:10 PM
  • Do you know any website to upload files to? I have finished the extension, but I just need to find a place to put it. But before I put it up anywhere, is there anything else you'd like me to add to this extension?
    ~~AirWaves!!~~
    Saturday, April 16, 2011 7:26 AM
  • No i dont think there is anything in particular that needs to be added. Thanks. And im not sure about file host at the moment. Can you share a file on windows skydrive? make a new folder and make it public? or a site like http://www.yourfilehost.com/flash_upload.php ?

    You could email it to me on the address in my sig if you struggle to find an upload site... I will look around for one suitable for th file type.


    Alex Warrington - alex.warrington@live.co.uk
    Saturday, April 16, 2011 10:59 AM