Java Color Distance colordiffsq(int rgb0, int rgb1)

Here you can find the source of colordiffsq(int rgb0, int rgb1)

Description

Computes squared euclidian distance of two RGB color values.

License

Apache License

Parameter

Parameter Description
rgb0 First color value.
rgb1 Second color value.

Return

Squared Euclidian distance between the two color values.

Declaration

public static float colordiffsq(int rgb0, int rgb1) 

Method Source Code

//package com.java2s;
/*/*from  w ww .ja v  a 2  s . c o m*/
 Copyright 2005, 2006 by Gerald Friedland and Kristian Jantz

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

public class Main {
    /**
     * Computes squared euclidian distance of two RGB color values.
     * <P>
     * Note: Faster to compute than colordiff
     *
     * @param rgb0 First color value.
     * @param rgb1 Second color value.
     * @return Squared Euclidian distance between the two color values.
     */
    public static float colordiffsq(int rgb0, int rgb1) {
        final int rDist = getRed(rgb0) - getRed(rgb1);
        final int gDist = getGreen(rgb0) - getGreen(rgb1);
        final int bDist = getBlue(rgb0) - getBlue(rgb1);
        return rDist * rDist + gDist * gDist + bDist * bDist;
    }

    /**
     * Returns the red component of an (A)RGB value.
     *
     * @param rgb An (A)RGB color value.
     * @return The red component, ranging from 0 to 255.
     */
    public static int getRed(int rgb) {
        return (rgb >> 16) & 0xFF;
    }

    /**
     * Returns the green component of an (A)RGB value.
     *
     * @param rgb An (A)RGB color value.
     * @return The green component, ranging from 0 to 255.
     */
    public static int getGreen(int rgb) {
        return (rgb >> 8) & 0xFF;
    }

    /**
     * Returns the blue component of an (A)RGB value.
     *
     * @param rgb An (A)RGB color value.
     * @return The blue component, ranging from 0 to 255.
     */
    public static int getBlue(int rgb) {
        return (rgb) & 0xFF;
    }
}

Related

  1. colorDiff(int a, int b)
  2. colorDiffAlpha(int a, int b)
  3. colorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
  4. colorDistance(final double r1, final double g1, final double b1, final double r2, final double g2, final double b2)
  5. colorDistance(final float[] lab1, final float[] lab2)
  6. colorDistance(final float[] lab1, final float[] lab2)