?
Solved

Compare colours to get difference...

Posted on 2005-04-06
8
Medium Priority
?
359 Views
Last Modified: 2008-01-16
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
Comment
Question by:pyromatt
  • 4
  • 2
  • 2
8 Comments
 
LVL 76

Expert Comment

by:David Lee
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

by:David Lee
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

by:rcrux
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:pyromatt
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

by:
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

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

Author Comment

by:pyromatt
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

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

615 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question