Author Topic: Convert 3 r,g,b to one?  (Read 843 times)

B+

• Full Member
• Posts: 215
Convert 3 r,g,b to one?
« on: September 02, 2016 »
There must be a way to convert 3 RGB integers to one for set colori to use instead of set color?

I am looking for RGB2I(r, g, b) function.
B+

B+

• Full Member
• Posts: 215
Re: Convert 3 r,g,b to one?
« Reply #1 on: September 03, 2016 »
Oh, set colori wont take a hex string nor can it be converted to usable integer eg  set colori int(hex\$)

So for converting RGB numbers to hex constants there is this:
Code: [Select]
`'RGB to hex.txt for Naalaa [B+=MGA] 2016-09-03'For constant settings'convert color in rgb format (r,g,b) to hex format 0xabcdefset window 100, 40, 400, 300OK = 1while OK set colori 0x000000 cls set colori 0xffffff set caret 20, 20 wln " *** Convert RGB to HEX for set colori *** " wln write "Enter R value < 256, R = " r = rln(3) write "Enter G value < 256, G = " g = rln(3) write "Enter B value < 256, B = " b = rln(3) if r > 255 or g > 255 or b > 255 if r > 255 or r < 0 then wln "error: r too high or low" if g > 255 or g < 0 then wln "error: g too high or low" if b > 255 or b < 0 then wln "error: b too high or low" else set caret 20, 125 ; wln "set color " + str(r) + ", " + str(g) + ", " + str(b) set color r, g, b draw rect 300, 90, 50, 50, true set colori 0xffffff set caret 20, 200 hexConvert\$ = "0x" + H2\$(r) + H2\$(g) + H2\$(b) wln "RGB numbers converted to hex is " + hexConvert\$ endif set caret 20, 240 wln "Do another? Enter y for yes... " ans\$ = rln() if ans\$ <> "y" then OK = falsewendfunction H2\$(n255) digits\$ = "0123456789abcdef" d1\$ = mid(digits\$, int(float(n255)/16.0), 1) d2\$ = mid(digits\$, n255 % 16, 1) return d1\$ + d2\$endfunc`
B+

Marcus

• Hero Member
• Posts: 549
Re: Convert 3 r,g,b to one?
« Reply #2 on: September 05, 2016 »
Code: [Select]
`c = (r SHL 16) + (g SHL 8) + bset colori c`

/on a trolley
« Last Edit: September 05, 2016 by Marcus »
.\\\opz

Marcus

• Hero Member
• Posts: 549
Re: Convert 3 r,g,b to one?
« Reply #3 on: September 05, 2016 »
And other way around would be something like:

Code: [Select]
`r = (c SHR 16) AND 0xffg = (c SHR 8) AND 0xffb = c AND 0xff`
.\\\opz

B+

• Full Member
• Posts: 215
Re: Convert 3 r,g,b to one?
« Reply #4 on: September 05, 2016 »
Thanks! Once again much more elegant solution.

But apparently we cant set these under constants:
but works under visible:
Code: [Select]
`constant: 'note = sign optionalMIDX = 400YMAX 600 'XMAX = MIDX * 2   'Message:'ERROR On line 3:' Expected integer constant 'even when I remove * 2'Lesson:' constant accepts no expressions including just an integer variable  visible:' So colori constants have to go under visible and thus are not protected from changeXmax = MIDX * 2Red = Ci(255, 0, 0)RedLow = Ci(64, 0, 0)Yellow = Ci(255, 255, 0)Blue = Ci(0, 0, 255)BrightWhite = Ci(255, 255, 255)hidden:set window 100, 40, Xmax, YMAXset caret MIDX, YMAX/2wln "Hi"set colori Redwln "This should be in color Red."set colori Yellowwln "This should be in color Yellow."set colori Bluewln "And this should be in Blue."set colori BrightWhitedraw rect 10, 10, 300, 100, trueset colori RedLowset caret 20, 50wln "This should be dark Red on White."wait keydownfunction Ci(r, g, b) return (r SHL 16) + (g SHL 8) + bendfunc`
B+

B+

• Full Member
• Posts: 215
Re: Convert 3 r,g,b to one?
« Reply #5 on: September 05, 2016 »
Funny, I thought I had tried something like this originally. And I thought it did not work because colori required a hex format.
Must have been some other goofy mistake because it is working now.

Code: [Select]
`function Ci(r, g, b) return 256 * 256 * r + 256 * g + b 'return (r SHL 16) + (g SHL 8) + bendfunc`
Code: [Select]
`'RGB to Big Integer.txt for Naalaa [B+=MGA] 2016-09-05'For constant settings'convert color in rgb format (r,g,b) to one Big Integerset window 100, 40, 400, 300OK = 1while OK set colori 0x000000 cls set colori 0xffffff set caret 20, 20 wln "Convert RGB to one big integer for set colori" wln write "Enter R value < 256, R = " r = rln(3) write "Enter G value < 256, G = " g = rln(3) write "Enter B value < 256, B = " b = rln(3) if r > 255 or g > 255 or b > 255 or r < 0 or g < 0 or b < 0 if r > 255 or r < 0 then wln "error: r too high or low" if g > 255 or g < 0 then wln "error: g too high or low" if b > 255 or b < 0 then wln "error: b too high or low" else set caret 20, 125 ; wln "set color " + str(r) + ", " + str(g) + ", " + str(b) set color r, g, b draw rect 300, 95, 50, 50, true convert = Ci(r, g, b) set colori convert draw rect 10, 160, 380, 50, true set colori Ci(255, 255, 255) set caret 20, 220 wln "RGB Big integer is " + str(convert) endif set caret 20, 260 wln "Do another? Enter y for yes... " ans\$ = rln() if ans\$ <> "y" then OK = falsewendfunction Ci(r, g, b) return 256 * 256 * r + 256 * g + b 'return (r SHL 16) + (g SHL 8) + bendfunc`
B+