Purely as a matter of curiosity if white is the opposite of black, the what is the opposite of blue? Or red?

Cheers!

Solved

Posted on 2005-04-06

Hi,

I need a function that takes in two colour values (long) and returns the an integer represtenting the similarity between the colours...

I.E, Public Function ColourCompare(Col1 as Long, Col2 as long) as Integer

The returned integer should be a percentage of how similar the colours are. In other words, if Col1 is Black and Col2 is White, then 0 should be returned as the colours are completely opposite and so there is a 0% similarity. If Col1 and Col2 are both Red then 100 should be returned as the colours are identical and so are 100% similar.

Matt.

I need a function that takes in two colour values (long) and returns the an integer represtenting the similarity between the colours...

I.E, Public Function ColourCompare(Col1 as Long, Col2 as long) as Integer

The returned integer should be a percentage of how similar the colours are. In other words, if Col1 is Black and Col2 is White, then 0 should be returned as the colours are completely opposite and so there is a 0% similarity. If Col1 and Col2 are both Red then 100 should be returned as the colours are identical and so are 100% similar.

Matt.

8 Comments

Purely as a matter of curiosity if white is the opposite of black, the what is the opposite of blue? Or red?

Cheers!

You can try using this code. The problem is that the difference is so large or so small in most cases that the result is usually 0 or 100. All this does is find the larger and smaller of the two values, then divide the smaller by the larger, and multiply the result by 100 to get a percentage of the smaller colour to the larger colour.

Function ColourCompare(lngColour1 As Long, lngColour2 As Long) As Integer

Dim lngLarger As Long, _

lngSmaller As Long, _

lngValue As Long

If lngColour1 = lngColour2 Then

ColourCompare = 100

Else

lngLarger = Max(lngColour1, lngColour2)

lngSmaller = Min(lngColour1, lngColour2)

lngValue = (lngSmaller / lngLarger) * 100

ColourCompare = lngValue

End If

End Function

Function Max(varValue1 As Variant, varValue2 As Variant) As Variant

Max = IIf(varValue1 > varValue2, varValue1, varValue2)

End Function

Function Min(varValue1 As Variant, varValue2 As Variant) As Variant

Min = IIf(varValue1 < varValue2, varValue1, varValue2)

End Function

I'm not gonna tell you how to code it, because I'm a bit short of time... but I can drop my idea...

1) First you should split the colors to RGB format...

2) To each color pair you shold do the falowing:

- find the absolute (ABS) difference between them

- You know the maximum difference possible is 255 (100%) so the difference you calculated should be x%

3) after you've done this you get r%,g% and b%

4) cal the average and that's it

just an idea that popped in my mind

give it a try 'cause I ain't got no use for it

Cheers

Yeh, that was the method i was thinking of, but ive no idea how to code that... the main problem being converting longs into RGBs.

BlueDevilFan:

Thanks, ill give that a go and get back to you.

I coded it and put it on my site http://www.rc-dev.com.

Please go there, access the code library, the function is under VB6->Functions->Graphics.

Knock yoursefl out..

I tried the first solution with the values rgb(255,255,0) and rgb(251,253,3) which are obviously quite similar, i worked it out manually to be nearly 99% accurate, but it returned 25%. The second solution worked great, returning 98.8....%

Thanks for your help tho BDF, much appreichiated :)

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

use like in If/Then vba | 4 | 43 | |

VB6 convert MSHFlexgrid1 cells no2 | 2 | 34 | |

Need VBS version of Excel date conversion | 4 | 43 | |

Concatenate 2 userform comboboxes to a userform text box | 4 | 42 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**18** Experts available now in Live!