Solved

# Compare colours to get difference...

Posted on 2005-04-06
351 Views
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.
0
Question by:pyromatt

LVL 76

Expert Comment

Hi pyromatt,

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

Cheers!
0

LVL 76

Expert Comment

pyromatt,

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
0

LVL 1

Expert Comment

HI,

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
0

Author Comment

rcrux:
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.
0

LVL 1

Accepted Solution

ok... ok...
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..
0

LVL 1

Expert Comment

I called it colorCompare.
Please tell me if it works for you... or if it needs revision! ;P
0

Author Comment

THATS PERFECT!!! Thanks!

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 :)
0

LVL 1

Expert Comment

you're welcome
keep coding..
0

## Featured Post

### Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…