HEX COLOR

OK... I'm tryin to single out each color (red, green, blue) from a hex color string... here is my code:

sub on_objecttimer456
'get the hexidecimal number of the current color of text
hexcolor = hex(me.textcolor)
'find each color
red = right(hexcolor,2)
green = mid(hexcolor,2,2)
blue = left(hexcolor,2)
end sub

My problem is that if hexcolor has any zeros in it, they don't seem to wanna show in hexcolor... like it auto-trims or somethin...
example:
sub on_objecttimer456
me.textcolor = RGB(255,0,0)
'hexcolor of that should be FF0000
hexcolor = hex(me.textcolor)

'result of me.textcolor value is 255
msgbox me.textcolor

'result of hex(me.textcolor) is FF (not FF0000)
msgbox hexcolor
end sub
1,119 views 9 replies
Reply #1 Top

Maybe if you convert the value into a text string not an integer.

Reply #3 Top

I haven't played a whole lot with DX, let me try a few things.

BTW: your code is screwed up...  It make no sense anyway as far as I understand DX script.

You wrote "sub on_objecttimer456", but I think it should be "sub Object_onTimer456".  You did define the timer first, right? ex: "Object.SetTimer 456, 10000". And I'm not sure how you are actually executing this script. Do you have actually two onTimer events? Not sure that would work.

Reply #4 Top

OK, here is what I did.  I probably cheated, there mist be a better way, but this works anyway.
The problem is that the Hex function seem to drop the trailing zeros. I looked it up and it seems to be a VBS thing, not specificly DX.  Anyway, I'm just adding back the zeros that got dropped:

 

Sub Object_OnScriptEnter
  Me.textcolor = RGB(255,0,0)
    HexValue = Hex(Me.textcolor)
    thislength = Len(HexValue)
   
    If (thislength = 1) Then
     HexValue = HexValue & "00000"
    ElseIf (thislength = 2) Then
     HexValue = HexValue & "0000"
    ElseIf (thislength = 3) Then
     HexValue = HexValue & "000"
    ElseIf (thislength = 4) Then
     HexValue = HexValue & "00"
    ElseIf (thisLength = 5) Then
     HexValue = Hexvalue & "0"
    End If
      
  red = left(HexValue,2)
  green = Mid(HexValue,3,2)
  blue = right(HexValue,2)    

    
    msgbox "R:" & red & " G:" & green & " B:" & blue
   
End Sub


[Message Edited]
Reply #5 Top
well, you could use a loop and add a zero until the length is 6.

for(var i = 0 HexValue.length == 6 i++)
HexValue += "0"

I leave it to you to convert this to VBScript.. I'm not fond of VB's syntax and so avoid it at all costs..
Reply #6 Top
You should definitely not fill till you have six digits. Hexadecimal colour description consists of three two digit numbers (rrggbb), not one six digit number. Just adding a zero to the end of the string would produce rubbish.
Reply #7 Top
Wait....nevermind.
[Message Edited]
Reply #8 Top
Thanks guys... I was able to use some of your ideas to get it working... I tested out that script a little more and figured out that if you have blue the HexValue code puts out FF0000 (which is bassackwards in my book RRGGBB)
so instead of adding the zeros to the end of it, I added it to the front... and instead of red ing the first two digits on left... i the first two digits on right... here is what I mean:

Sub Object_OnTimer54987
Me.textcolor = RGB(171,12,255)

HexValue = Hex(Me.textcolor)
thislength = Len(HexValue)

If (thislength = 1) Then
HexValue = "00000"& HexValue
ElseIf (thislength = 2) Then
HexValue = "0000"& HexValue
ElseIf (thislength = 3) Then
HexValue = "000" & HexValue
ElseIf (thislength = 4) Then
HexValue = "00"& HexValue
ElseIf (thisLength = 5) Then
HexValue = "0" & Hexvalue
End If

red = right(HexValue,2)
green = Mid(HexValue,3,2)
blue = left(HexValue,2)
msgbox HexValue & "," & red & "," & green & "," & blue



End Sub



seems to be working...
Reply #9 Top
whoa... lots of typo's ... "and instead of READING the first two digits on the left... it reads the first two on right"