The easiest way to do it is to simply extract the individual RGB values from the color, average them, and create a new color using the average for all three components. Simple, but it is incorrect, visually, as far as I know. Someone else may be in here at some point to give you a better solution.
In the graphics biz, the standard way is to calculate the luminance value for each point and then map it over to an array of
RGB(0,0,0) -> RGB(255,255,255).
I've posted this code about four-five times now - do a search on
this forum for the word 'luminance' Don't search on my name - my account got hosed and the search engine thinks my old posts belong to 'Guest' or something.
you mean your post?
i just showed a routine i made to make it easy for him...
the other 2 ones didnt work properly...
the first one took the average from the lowest and highest value from r, g and b.....
the other one changed to less colors...
Just look closely at the name of the function : Black & White, it means NO GREY. (2 Colors : 1. White, 2. Black )
I've build this little Module on the principles that :
(Recall)
RGB(0,0,0) = BLACK
RGB(255,255,255) = WHITE
So a colour is coded on 3 Bytes stored in a long (4 Bytes)
R G B R G B
a Colour is coded to &H 00 00 00 to & H FF FF FF
I extract each colors (R, G, B) by an AND and see if each of them are bigger than 127 (Brithness indeed, no ?)
Your code is great, and helpfull, thanks !
But what do you think about writing a function like :
GreyImage(pPictureBox as PictureBox, byval GreyLevel as byte)
Where GreyLevel is the accuracy in grey :
Example :
(Grey level cannot be 0 or 1), because
GreyLevel =2, it will be black and white only
GreyLevel =3 will be black and white and Perfect Grey
gr = (0.3333*clr.red+0.59*clr.green+0.11*clr.blue)/3
' You need to refine this part, it doesn't work this way
For i = 1 To GrayLevel-1
If gr < (255 / GrayLevel)*i Then
gr = (255/GrayLevel)*i-1
Goto Done
End If
Next i
gr = 255
Done:
clr.red = clr.green = clr.blue = gr
px.color = clr
Next px
The problem of this spacing algorithm is that every one that I could come up when thinking about this would either not work with more than 2 greys or exactly black & white.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
There are many shades of gray, computers usually display 256 (RGB(0,0,0) to RGB(255,255,255)).
When you have a color picture you usually take the average of the color components to get the shade of grey this particular color correspondends to. But it would be wrong to simply use every color component the same (0.3333*r+0.3333*g+0.3333*b), because the human eye values the components differently. A bright green looks far brighter than a bright blue. Therefore you have to use a weighted average: (0.3333*r+0.59*g+0.11*b). I have to correct myself, there is no /3 there.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
If you want a black-and-white artistic feeling, use one of the excellent grayscale code snippets in this thread, and if R, G, and B are under 128, make all 0 (black). If they're 128 or over, make them 255 (white).
If you want something more accurate, you'll have to look into things like dithering and error defusion.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
(Just a heads-up)