PDA

Click to See Complete Forum and Search --> : They dont listen to me too :(


iScanning
Mar 16th, 2001, 05:51 PM
In the general forum got nno answer hope you can really help me out! (Desperate)

Here i go:

I'm writting a program that has a form with many random colors in a picture box!

What i m trying to accomplish is to after the user clicks on a color the program tells wich color that is and if its light or dark color!

Thing is there are millions of colors, so i would like to write a function to help me out!

I thing that if i use the HSL i can know wich color it is but i dont know how since i couldn't find a proportion with the rgb values, like if Red > 100 and blue < 200 = something!

I know how to get the long and convert it to RGB values but i'm looking to find a relation, see? U think HSL is a possible one?

PLease give me some hints!

Thanks!!!!

Fox
Mar 16th, 2001, 06:38 PM
That's easy:


Sub ColorToRGB (ByVal Color As Long, R As Integer, G As Integer, B As Integer)
R = Color Mod 256
Color = Color \ 256
G = Color Mod 256
Color = Color \ 256
B = Color Mod 256
End Sub

'Code improved by vBulletin Tool (http://orion.spaceports.com/~mccloud/vbtool.zip) (Save as...)

Sastraxi
Mar 16th, 2001, 06:49 PM
After doing that, you could use....

Function ISLIGHT(R,G,B) as Boolean
Const MaxCapac=255+255+255

If R+G+B<=Int(MaxCapac/2)

End Function

iScanning
Mar 16th, 2001, 09:16 PM
I swear i looked at this 2 codes for a long lonng time and i'm even afraid to ask cause of my ignorance but i didn't understand the "logic" i guess thats the word of both helps you gave!

I'm very sorry, please be more accurate and explain ;)

Thankx for trying to help though!

Lord Orwell
Mar 16th, 2001, 09:35 PM
ok they are assuming that you know how to get the pixel color under the cursor.
You take a long color value ok?
It is combined of 4 bytes. Each byte represents these 4 things:
byte 1: unused
byte 2: blue portion of color in the value range of 0-255
byte 3: green portion in range of 0-255
byte 4: red portion in range of 0-255

The first code posted is used to get the red, green, and blue portions of the color from the long value with math tricks.

The 2nd code works like similar to this one:
Brightnessofcolor = int((red+green+blue) / 3)
which will return a number in the range of 0 to 255.
This is the brightness of the color. It should be pointed out however that the green portion of the color is actually about 2x the brightness of the red and blue part. So your reading would have to take that into account if you wanted proper brightness checking.
perhaps this code:
Brightnessofcolor = int((red + green + green + blue) / 4)
A suitable mod could be made for blue as well, since in my opinion blue is darker than red. Im not bsing about green being brighter either. The value of the green component for standard green is 128. The value for the blue component for standard blue is 255.

So after you get your brightness value, a simple matter to split it between light and dark:
if Brightnessofcolor > 128 then
msgbox "bright!"
else
msgbox "dark!"
end if


I never saw you post in the general forum...

Sastraxi
Mar 17th, 2001, 09:05 AM
Hey, ya cant blame me for having a 5 second post :)

Happy Coding!

iScanning
Mar 17th, 2001, 09:27 AM
That's great thanks, the light-dark matter is solved!

By the way i use pset to get the long color, is it accurate?
i'd like to try fox's example but if that sub is to try and find the R G B values hoow come its in a form of a sub,cause then i have to insert the R G B values before i know them?

Humm not usest to this VB code about graphics!!!

Now all i need is too find the same thing you guuys helped but to find out the color, meaning:


function DefineColor(R as integer, G as Integer, B as Interger) as string

Select case RGB
Case r =< 100 and g => 200 and b >50
'then use the brightnessof color function and define wich color it is
If BrightnessOfColor > 128 Then
MsgBox "bright!"
Else
MsgBox "dark!"
End If
definecolor is Light Yealow' for example of course
case ....

end select



P.S. - I really would like to know the function to figure out the HSL from a RGB value

P.S. 2 - I posted as Defining color
Thanks a lot!!!!!!!

Sastraxi
Mar 17th, 2001, 09:38 AM
Use API GetPixel and if you need SetPixelV. the V is important cause it is faster, since it doenst return the colour value.

iScanning
Mar 17th, 2001, 11:11 AM
Ok, i thinkk i havent made myself clear!

My problem is not about getting the pixel as long or "translating" it to rgb, i want to write a function where i have a list of combinations of RGB values that will return the names: light BLUE, Dark Brown, see?

But since there are millions of colors and i m certainly not gonna write: 1,1,1 = black , 1,2,2 = black and so on to every color that we know of. But anyways i think i can find a way to wrrite coode just as the brightness example if i know how can i find the HSL(Hue,Saturation...) from a RGB color! Can you guys tell me how?

Because the rgb is very vague(dont know if it says like that in english) but it will be easy to me if i know the HSL since with the hue usually you can get the color, like: if hue =>0 and < 10 then color x, if hue >10 < and < 20 then color = y

Thanks for the patience!!!

Lord Orwell
Mar 17th, 2001, 01:29 PM
I should like to mention that i gave some slightly inaccurate data to you. I said that bit4 is unused. This isn't entirely true. If you use 32-bit graphics, the 4th byte is used to store alpha information.