Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
360tv
Streaming Star

Passing a hex int via XML

I'm trying to draw some Rectangles from data received by roXMLElement. the problem I'm having is dictating the color since it doesn't work as simply as setting colors on orImageCanvas. I can't seem to get it the "FFFFFFFF" string to convert to &hFFFFFFFF or whatever data type I need for DrawRect(). I get a type missmatch. Suggestions?
0 Kudos
8 REPLIES 8
EnTerr
Roku Guru

Re: Passing a hex int via XML

"360tv" wrote:
I'm trying to draw some Rectangles from data received by roXMLElement. the problem I'm having is dictating the color since it doesn't work as simply as setting colors on orImageCanvas. I can't seem to get it the "FFFFFFFF" string to convert to &hFFFFFFFF or whatever data type I need for DrawRect(). I get a type missmatch. Suggestions?

If i read it right, you are getting some hexadecimal string literals from outside and want to convert them to RGBA int for call to DrawRect(). I had three ideas and tried them but they all failed:
  1. Use val() to convert string to number - fails, does not support &hFF format (somewhat excusable, since there is no hexadecimal format for floats which it is defined as)

  2. use int() - fails, since it takes int and not string

  3. use roByteArray.fromHexString() .toSignedLong() - this one almost worked but ultimately failed because it returns long in little-endian order where RGBA from what i see is in network-order (big-endian). And oh joy, there is no StrReverse()

Looking forward to hear if there is an elegant solution.
If not, seems to me would be a good idea to make
  • val() accept hexadecimal-int strings,

  • int() to take dec/hex strings

  • roString.toInt() to take hex

  • maybe a way for roByteArray to deal big-endian longs
0 Kudos
360tv
Streaming Star

Re: Passing a hex int via XML

Yeah, you read it right.

But... so wait...

Are you saying that this roByteArray.fromHexString() .toSignedLong() trick you were trying would work but it'd return values in the order of ABGR instead of RGBA?IF so couldn't I just put in the hex values in the order of ABGR so that it would eventually get put into drawrect as RGBA?

Otherwise, I'm probably going to have to build a function holding a katrillion color values and deal with the hassle of publishing a new version of my channel when I want to add a color. (Because I'm using branded colors like USA Today Blue = #009BFF, et cetra...)
0 Kudos
TheEndless
Channel Surfer

Re: Passing a hex int via XML

bslCore.brs includes a HexToInteger function (http://sdkdocs.roku.com/display/sdkdoc/ ... gasInteger😞
Function HexToInteger(hex_in)

out=0
hex=LCase(hex_in)

for i=1 to len(hex)
dec1=asc(mid(hex, i, 1))
if dec1>=asc("0") AND dec1 <=asc("9") then dec1=dec1-asc("0") else dec1=dec1-asc("a")+10
out=out*16+dec1
end for

return out
end function
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Passing a hex int via XML

"360tv" wrote:
Are you saying that this roByteArray.fromHexString() .toSignedLong() trick you were trying would work but it'd return values in the order of ABGR instead of RGBA?IF so couldn't I just put in the hex values in the order of ABGR so that it would eventually get put into drawrect as RGBA?

yes, it should work if you pass ABGR. if you have the freedom server-side, you could as well provide the colors in decimal form there.
0 Kudos
360tv
Streaming Star

Re: Passing a hex int via XML

Thanks EnTerr but... I should I have known TheEndless would come along and give the exact answer I'm looking for. Works SO perfectly!!
0 Kudos
EnTerr
Roku Guru

Re: Passing a hex int via XML

"TheEndless" wrote:
bslCore.brs includes a HexToInteger function (http://sdkdocs.roku.com/display/sdkdoc/ ... gasInteger)

I see. So there is a documented solution which is slow and error-prone (no error detection - be very careful not to pass string with space in the beginning or end, since that will count as -55 pts).

I did search the documentation first of course but was looking for "hexadecimal" and did not see it. Did not occur to me it would be referred to as "hex string" <sigh>
0 Kudos
360tv
Streaming Star

Re: Passing a hex int via XML

Yeah, trust me, I was turning the docs inside out looking for that. I didn't see it either. But I've already put it in my code and got it working in about two minutes. The documentation can be vauge and confusing sometimes. I've been learning brightscript for about a year now and this "bslCore.brs"I just found out about last week.
0 Kudos
EnTerr
Roku Guru

Re: Passing a hex int via XML

Here is another implementation, it is ~3 times faster than the one posted above:
function HexToInteger3(hex_in)
bArr = createobject("roByteArray")
if len(hex_in) mod 2 > 0
'fix for fromHexString() malicious silent failure on odd length
hex_in = "0" + hex_in
end if
bArr.fromHexString(hex_in)
out = 0
for i = 0 to bArr.count()-1
out = 256 * out + bArr[i]
end for
return out
end function

Testing was done with 8-digit hexadecimals that would be typical use (like RGBA), highest number of calls in a second in repeat runs. The new version on 3100 nets at 72 microseconds per call vs the one given at 209 usec. On Roku3 is 4x (13us vs 52us). This is substantial so i suggest somebody from RokuCo replacing the HexToInteger implementation in bslCore.brs with this one.

Odds and ends: in our new series "turning lemons into lemonade", watch how because of a quirk in roByteArray.fromHexString implementation the new function can parse hex-formats that the "old" one cannot:
BrightScript Debugger> ? hexToInteger3(" fff")  'leading spaces
4095
BrightScript Debugger> ? hexToInteger3("$fff") 'Turbo Pascal
4095
BrightScript Debugger> ? hexToInteger3("0xFFF") 'Unix/C
4095
BrightScript Debugger> ? hexToInteger3("&hFFF") 'Microsoft BASIC
4095
BrightScript Debugger> ? hexToInteger3("#0000FF") 'HTML blue
255
BrightScript Debugger> ? hexToInteger3("U+20AC") 'Unicode €
8364
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.