Solved

# Compare colours to get difference...

Posted on 2005-04-06
Medium Priority
359 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
• 4
• 2
• 2

LVL 76

Expert Comment

ID: 13721862
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

ID: 13722216
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

ID: 13722415
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

ID: 13722451
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

rcrux earned 1000 total points
ID: 13722568
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

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

Author Comment

ID: 13722618
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

ID: 13722643
you're welcome
keep coding..
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't interesteâ€¦
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describesâ€¦
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can launâ€¦
###### Suggested Courses
Course of the Month8 days, 1 hour left to enroll