[RESOLVED] Color Picker Control... :P
Ok so...(mm i think i use triple point too much xD) i'm making a simple color picker control for my App and i have 2 questions:
1) Since i couldn't write my own Hex2RGB function due to many errors :P (i tried using CByte() but couldn't get it to work properly)
so i found this code:
Code:
Public Function HEXCOL2RGB(ByVal HexColor As String) As String
'The input at this point could be HexColor = "#00FF1F"
Dim Red As String
Dim Green As String
Dim Blue As String
HexColor = Replace(HexColor, "#", "")
'Here HexColor = "00FF1F"
Red = Val("&H" & Mid(HexColor, 1, 2))
'The red value is now the long version of "00"
Green = Val("&H" & Mid(HexColor, 3, 2))
'The red value is now the long version of "FF"
Blue = Val("&H" & Mid(HexColor, 5, 2))
'The red value is now the long version of "1F"
HEXCOL2RGB = RGB(Red, Green, Blue)
'The output is an RGB value
End Function
But the problem is that it adds a # to it and i don't want it to do so... i tried removing some parts of the code... but *****s up* the function.
Can anyone help me on this or provide me with a better function??
2) My app has xp styles "installed" on it but if i make a control... when i use it will it take the style? or do i have to do something like the things you do to the application itself?
Thx in advance!!
Re: Color Picker Control... :P
Here's something
Code:
Public Function HEXCOL2RGB(ByVal sHexColor As String) As Long
Dim TmpDbl As Double, TmpLng As Long
HEXCOL2RGB = -1 'this indicates a fail
sHexColor = UCase$(Trim$(sHexColor)) 'remove spaces and force case
If Left$(sHexColor, 2) <> "&H" Then sHexColor = "&H" & sHexColor 'Add Hex prefix if needed
If IsNumeric(sHexColor) Then 'Can be coerced to a number
TmpDbl = CDbl(sHexColor) 'Using double to avoid overflow
Select Case TmpDbl
Case Is < 0 'out of range, skip
Case Is < &H1000000 'in the range 0-&hffffff
TmpLng = TmpDbl
'Convert from BigEndian to LittleEndian
HEXCOL2RGB = (TmpLng And &HFF&) * &H10000 Or (TmpLng \ &H10000) Or (TmpLng And &HFF00&)
End Select
End If
End Function
It's a little more robust than your function in how it checks the string the following would be valid input.
" 123456 "
"&h8080ff"
" AbCdEf "
It also returns a long rather than a string. If the input is invalid (not a number or out of range) the return is -1
Edit: by the way, the common dialogue control has a colour picker built in
Re: Color Picker Control... :P
Quote:
Edit: by the way, the common dialogue control has a colour picker built in
I know it has but i prefer mine :P
Well it wasn't my function... i just found it as i said...
And how would i go about putting the results on three text boxes 1 for red m 1 for green and 1 for blue??
Thx in advance!
Re: Color Picker Control... :P
Code:
Public Sub SplitColour(Colour As Long, Red As Byte, Green As Byte, Blue As Byte)
Red = Colour And &HFF&
Green = (Colour \ &H100&) And &HFF&
Blue = (Colour \ &H10000) And &HFF&
End Sub
Re: Color Picker Control... :P
i can't get it to work :(
can u try it and reupload if u success pls??
here is my project...
Thx m8!
Re: Color Picker Control... :P
Because it requires the return of three values I made it a sub, you would use it like this...
Code:
Dim Red As Byte, Green As Byte, Blue As Byte
SplitColour 12648200, Red, Green, Blue
Debug.Print Red, Green, Blue
Alternatively you could define a UDT of three bytes and make it a function which returns the UDT
Re: Color Picker Control... :P
The built in ColorPicker control doesnt work for you in your situation?
Re: Color Picker Control... :P
Yes it works robdog but i wanted to use mine :)
@Milk!
Thx man worked perfect!!
Really thx!
Re: [RESOLVED] Color Picker Control... :P
Ok np. I just wasnt sure if you knew about it as it can be shown in advanced more to open the right side and expose the color picker spectrum. also you can load the custom colors boxes with your own colors.