Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Hex to Dec for you

Author
Message
damothegreat
User Banned
Posted: 18th Oct 2016 11:24
Hello

There is a hex function to convert decimal to hex - great

But what if we like to use functions that primarily use colours as decimals.

Like, How to work out a Decimal from Hex (RRGGBB) hex coding.

Here is my function


// Project: Hex2Int
// Created: Damo 2016-10-17

// show all errors
SetErrorMode(2)
width=1024: height=768
// set window properties
SetWindowTitle( "Hex2Int" )
SetWindowSize( width, height, 0 )

// set display properties
SetVirtualResolution( width, height )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery

do
color=hex2dec("0000FF") // Blue
DrawEllipse(width/2, height/2,100,100,color,color,1)
DrawEllipse(random(0,width),random(0,height),50,50,hex2dec("FFAAAA"),hex2dec("FF0000"),1) //Red
DrawEllipse(random(0,width),random(0,height),50,50,hex2dec("FFFF00"),hex2dec("FFFF00"),1) //Yellow
DrawEllipse(random(0,width),random(0,height),50,50,hex2dec("FFFFFF"),hex2dec("FFFFFF"),1) //white
Sync()
loop


function hex2dec(colorinhex$ as string )

result as integer
result = 0

//loop through the string brought in and check each value in turn

for a=1 to len(colorinhex$)

// Grab a value in order of the string and work it out
value$=mid(colorinhex$, a, 1)

//Convert any A B C D E F to their correct base 16 value
if value$="A" or value$="a" then value$="10"
if value$="B" or value$="b" then value$="11"
if value$="C" or value$="c" then value$="12"
if value$="D" or value$="d" then value$="13"
if value$="E" or value$="e" then value$="14"
if value$="F" or value$="f" then value$="15"

// Calculate by * the value to the power of 16 of the current position
result=result+val(value$) * (16^(a-1))
next

// Return it back out to be able to use most AppGameKit functions..
// eg. DrawEllipse function uses pure decimal number for colours

endfunction result


Enjoy!

Thanks
Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Oct 2016 23:28
This might be easier

x = val("FF0000", 16)

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
damothegreat
User Banned
Posted: 19th Oct 2016 15:34
Thanks for that

Yeah I didn't look deeper into the Val function lol my bad

Cheers
Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS

Login to post a reply

Server time is: 2024-11-21 21:13:58
Your offset time is: 2024-11-21 21:13:58