Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 11th, 2005, 12:36 PM   #1
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
Talking hex and decimal RGB transforming please help

hi ,

im making this program that, by using the common color dialog i can select the color and it will return the rgb. it is for a subtitle script (SSA/ASS). so i can generate the colors easier as they work both in hex and plain rgb numbers. i have two option buttons one for the hex color and one for the decimal color, the hex or decimal color appears in a textbox (depending on the option selected). Anyway when i click the other option (when the color is in the text box) it needs to change to hex from decimal (i dont need help with that) but i dont have a clue how to get the hex number to decimal again. could you help me

thanks alot
cloud- is offline   Reply With Quote
Old Jan 11th, 2005, 2:27 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Try this:
Dim nHex As Integer, nDec As Integer
nHex = Hex(20)
nDec = Val("&H" & nHex)
The "&H" bit defines its hexadecimalness.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 11th, 2005, 7:06 PM   #3
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
RGB Stuff

Sorry do you just want that decimal Heximalizicallified because I believe that will actually produce the wrong colour. Colour selection dialogs return colours as longs, just like constants (vbwhite) and the rgb function. What you need to do is convert the Hex value into a long by means of the rgb function. The hex value will always be 6 characters long, and in the order R G B. The first two characters make up the Red channel (one byte /255), the next two the blue and so on. So what you actually do (sorry Samir... d'oh I mean er... ooble) is:
myvar = "FF0000" 'red
mylong = rgb(val("&H" & mid(myvar,1,2)),val("&H" & mid(myvar,3,2)),val("&H" & mid(myvar,5,2)))
Rory is offline   Reply With Quote
Old Jan 14th, 2005, 12:09 PM   #4
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
hi thanks alot for your replys
one thing though

heres the code i used

Private Sub Option1_Click()
If (Option2.Value = False) Then
If (Color = "Hex") Then
Dim myvar As String
Dim mylong As Long
myvar = Text1.Text
mylong = RGB(Val("&H" & Mid(myvar, 1, 2)), Val("&H" & Mid(myvar, 3, 2)), Val("&H" & Mid(myvar, 5, 2)))
Text1.Text = mylong
End If
Color = "Dec"
End If
End Sub

Private Sub Option2_Click()
If (Option1.Value = False) Then
If (Color = "Dec") Then
Text1.Text = Hex(Text1.Text)
End If
Color = "Hex"
End If
End Sub


color is some variable that determines if its hex or dec.

but for some reason it turns out to be different

like
color from color dialog = 14119408
click on hex option and it = D771F0
click decimal again and i get = 15757783
click hex again and i get = F071D7
then decimal again and i get what i started with = 14119408
and so on...

this cant be right am i doing something wrong

please help thankyou very much
cloud- is offline   Reply With Quote
Old Jan 15th, 2005, 10:57 AM   #5
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
RGB Problems

The only things I can see in your code are that hex(text1.text) should be hex(val(text1.text)) to prevent any possible run-time errors. Also it'd be better to use a boolean instead of colour.

What's happening is that the colour is being inverted - first click returns the inverse, second click returns the inverted inverse (the original). This is because the colour longs from the colour dialog seem to return RGB as little-endian and not big-endian like HTML colours. All you need to do is reverse your mid statements:
mylong = RGB(Val("&H" & Mid(myvar, 5, 2)), Val("&H" & Mid(myvar, 3, 2)), Val("&H" & Mid(myvar, 1, 2)))
Rory is offline   Reply With Quote
Old Jan 17th, 2005, 3:17 PM   #6
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road

thanks alot for the replys guys <- that rymes . lol
i thought of an easier way
i just set two vars from when i got the color back one
hex and one regualar value
so i never had to go through those changing problems



thanks again
cloud- is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC