J Code
J Code

Reputation: 464

Compare color shades?

I have two colors, how do I check if they are the same color but just a different shade? I've been trying but I cant seem to figure it out, I really don't know what I'm doing lol... This is what I have so far:

import java.awt.Color;

public class Sandbox {

    public Sandbox() {
        Color c = new Color(5349322);
        int r, g, b;
        r = c.getBlue();
        g = c.getGreen();
        b = c.getRed();
        System.out.println("Red: " +  r);
        System.out.println("Green: " + g);
        System.out.println("Blue: " + b);
    }

    private boolean FindColorTol(int intTargetColor, int Tolerance) {
        Color targetColor = new Color(intTargetColor);
        Color imgColor = new Color(5349322);
        int targetRED = targetColor.getBlue(),
            targetGREEN = targetColor.getGreen(),
            targetBLUE = targetColor.getRed(),
            imgRED = imgColor.getBlue(),
            imgGREEN = imgColor.getGreen(),
            imgBLUE = imgColor.getRed();

        return false;
    }

    private int getLargest(int...values) {
        int largest = 0;
        for(int i = 0; i < values.length; i++) {
            if(values.length > i + 1) {
                if(values[i] > values[i + 1]) 
                   largest = values[i];
               else 
                   largest = values[i + 1];
           }
       }
        return largest;
    }

    public static void main(String[] args) {
        new Sandbox();
    }
}

And also, why does Color.getRed(), return the value for blue and, Color.getBlue() returns the value for returns the value for red? I am using this to find RGB values: http://www.colorschemer.com/online.html

I am trying to use this to find a specified color within an image.

Upvotes: 0

Views: 381

Answers (2)

camickr
camickr

Reputation: 324197

Maybe HSL Color will help. Strictly speaking I think the Hue and Saturation would need to be the same values.

Upvotes: 0

r3mainer
r3mainer

Reputation: 24587

In colour theory, a shade is what you get by mixing a colour with different amounts of black. So you can easily check if two RGB triplets correspond to different shades of the same colour by normalizing their values

max1 = max(r1,g1,b1);
max2 = max(r2,g2,b2);
if ( approxEQ(r1/max1,r2/max2,DELTA) && 
     approxEQ(g1/max1,g2/max2,DELTA) && 
     approxEQ(b1/max1,b2/max2,DELTA) ) {

  /* Same colour, different shades */

}

(where, obviously, max(a,b,c) returns the largest of three parameters, and approxEQ(a,b,d) returns true if |a-b|≤d, or false otherwise.)

If you want to check for tints as well, you would be better off converting your RGB values to HSV or HSL.

Upvotes: 1

Related Questions