Your Digital Media Has Never Looked So Good

 
360tv
Topic Author
Posts: 78
Joined: Tue Sep 04, 2012 9:20 pm

Passing a hex int via XML

Wed Jan 22, 2014 2:23 am

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?
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing a hex int via XML

Wed Jan 22, 2014 3:30 pm

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
 
360tv
Topic Author
Posts: 78
Joined: Tue Sep 04, 2012 9:20 pm

Re: Passing a hex int via XML

Wed Jan 22, 2014 6:00 pm

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...)
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: Passing a hex int via XML

Wed Jan 22, 2014 6:17 pm

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)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing a hex int via XML

Wed Jan 22, 2014 7:17 pm

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.
 
360tv
Topic Author
Posts: 78
Joined: Tue Sep 04, 2012 9:20 pm

Re: Passing a hex int via XML

Wed Jan 22, 2014 7:35 pm

Thanks EnTerr but... I should I have known TheEndless would come along and give the exact answer I'm looking for. Works SO perfectly!!
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing a hex int via XML

Wed Jan 22, 2014 7:37 pm

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>
Image
 
360tv
Topic Author
Posts: 78
Joined: Tue Sep 04, 2012 9:20 pm

Re: Passing a hex int via XML

Wed Jan 22, 2014 8:42 pm

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.
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Passing a hex int via XML

Thu Jan 23, 2014 11:30 pm

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

Who is online

Users browsing this forum: No registered users and 4 guests