Compare colours to get difference...

Posted on 2005-04-06
Last Modified: 2008-01-16
 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.

Question by:pyromatt
    LVL 76

    Expert Comment

    by:David Lee
    Hi pyromatt,

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

    LVL 76

    Expert Comment

    by:David Lee

    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
            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
    LVL 1

    Expert Comment


    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


    Author Comment

    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.

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

    Accepted Solution

    ok... ok...
    I coded it and put it on my site
    Please go there, access the code library, the function is under VB6->Functions->Graphics.

    Knock yoursefl out..
    LVL 1

    Expert Comment

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

    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 :)
    LVL 1

    Expert Comment

    you're welcome
    keep coding..

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction In a recent article ( 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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now