Java Code Examples for java.awt.Color#getRGBColorComponents()

The following examples show how to use java.awt.Color#getRGBColorComponents() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ghidra   File: ColorUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * A method to produce a color (either black or white) that contrasts with the given color.
 * This is useful for finding a readable foreground color for a given background.
 * 
 * @param color the color for which to find a contrast.
 * @return the contrasting color.
 */
public static Color contrastForegroundColor(Color color) {
	float[] rgbs = new float[3];
	color.getRGBColorComponents(rgbs);
	int fR = rgbs[0] > 0.5 ? 0 : 1;
	int fG = rgbs[1] > 0.5 ? 0 : 1;
	int fB = rgbs[2] > 0.5 ? 0 : 1;

	// Note: the more accurate operation for calculating luminance is:
	// float gamma = 2.2;
	// float luminance = 0.2126 * pow(r, gamma) + 
	//                   0.7152 * pow(g, gamma) + 
	//                   0.0722 * pow(b, gamma);

	// less precise, faster calculation
	double luminance = 0.2126 * (fR * fR) + 0.7152 * (fG * fG) + 0.0722 * (fB * fB);
	Color foreground = Color.WHITE;
	if (luminance < 0.54) { // about half (a bit fudge, since we are approximating the pow())
		foreground = Color.BLACK;
	}
	return foreground;
}
 
Example 2
void emitPSColor(Color color) {
    if (color != null && color.equals(mColor) == false) {
        float[] rgb = color.getRGBColorComponents(null);

        /* If the color is a gray value then use
         * setgray.
         */
        if (rgb[0] == rgb[1] && rgb[1] == rgb[2]) {
            mPSStream.println(rgb[0] + SETGRAY_STR);

        /* It's not gray so use setrgbcolor.
         */
        } else {
            mPSStream.println(rgb[0] + " "
                              + rgb[1] + " "
                              + rgb[2] + " "
                              + SETRGBCOLOR_STR);
        }

        mColor = color;

    }
}
 
Example 3
void emitPSColor(Color color) {
    if (color != null && color.equals(mColor) == false) {
        float[] rgb = color.getRGBColorComponents(null);

        /* If the color is a gray value then use
         * setgray.
         */
        if (rgb[0] == rgb[1] && rgb[1] == rgb[2]) {
            mPSStream.println(rgb[0] + SETGRAY_STR);

        /* It's not gray so use setrgbcolor.
         */
        } else {
            mPSStream.println(rgb[0] + " "
                              + rgb[1] + " "
                              + rgb[2] + " "
                              + SETRGBCOLOR_STR);
        }

        mColor = color;

    }
}
 
Example 4
private static boolean compareWithTolerance(Color a, Color b, float delta) {
    float[] a_rgb = new float[3];
    a_rgb = a.getRGBColorComponents(a_rgb);
    float[] b_rgb = new float[3];
    b_rgb = b.getRGBColorComponents(b_rgb);

    for (int i = 0; i < 3; i++) {
        if (Math.abs(a_rgb[i] - b_rgb[i]) > delta) {
            return false;
        }
    }
    return true;
}
 
Example 5
protected void selectSolidBrush(Color color) {

        /* We only need to select a brush if the color has changed.
        */
        if (color.equals(mLastColor) == false) {
            mLastColor = color;
            float[] rgb = color.getRGBColorComponents(null);

            selectSolidBrush(getPrintDC(),
                             (int) (rgb[0] * MAX_WCOLOR),
                             (int) (rgb[1] * MAX_WCOLOR),
                             (int) (rgb[2] * MAX_WCOLOR));
        }
    }
 
Example 6
protected boolean selectStylePen(int cap, int join, float width,
                                 Color color) {

    long endCap;
    long lineJoin;

    float[] rgb = color.getRGBColorComponents(null);

    switch(cap) {
    case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
    case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
    default:
    case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
    }

    switch(join) {
    case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
    default:
    case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
    case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
    }

    return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
                           (int) (rgb[0] * MAX_WCOLOR),
                           (int) (rgb[1] * MAX_WCOLOR),
                           (int) (rgb[2] * MAX_WCOLOR)));
}
 
Example 7
/**
 * Set the GDI color for text drawing.
 */
protected void setTextColor(Color color) {

    /* We only need to select a brush if the color has changed.
    */
    if (color.equals(mLastTextColor) == false) {
        mLastTextColor = color;
        float[] rgb = color.getRGBColorComponents(null);

        setTextColor(getPrintDC(),
                     (int) (rgb[0] * MAX_WCOLOR),
                     (int) (rgb[1] * MAX_WCOLOR),
                     (int) (rgb[2] * MAX_WCOLOR));
    }
}
 
Example 8
protected void selectPen(float width, Color color) {

        float[] rgb = color.getRGBColorComponents(null);

        selectPen(getPrintDC(), width,
                  (int) (rgb[0] * MAX_WCOLOR),
                  (int) (rgb[1] * MAX_WCOLOR),
                  (int) (rgb[2] * MAX_WCOLOR));
    }
 
Example 9
/**
 * Set the GDI color for text drawing.
 */
protected void setTextColor(Color color) {

    /* We only need to select a brush if the color has changed.
    */
    if (color.equals(mLastTextColor) == false) {
        mLastTextColor = color;
        float[] rgb = color.getRGBColorComponents(null);

        setTextColor(getPrintDC(),
                     (int) (rgb[0] * MAX_WCOLOR),
                     (int) (rgb[1] * MAX_WCOLOR),
                     (int) (rgb[2] * MAX_WCOLOR));
    }
}
 
Example 10
protected boolean selectStylePen(int cap, int join, float width,
                                 Color color) {

    long endCap;
    long lineJoin;

    float[] rgb = color.getRGBColorComponents(null);

    switch(cap) {
    case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
    case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
    default:
    case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
    }

    switch(join) {
    case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
    default:
    case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
    case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
    }

    return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
                           (int) (rgb[0] * MAX_WCOLOR),
                           (int) (rgb[1] * MAX_WCOLOR),
                           (int) (rgb[2] * MAX_WCOLOR)));
}
 
Example 11
protected void selectPen(float width, Color color) {

        float[] rgb = color.getRGBColorComponents(null);

        selectPen(getPrintDC(), width,
                  (int) (rgb[0] * MAX_WCOLOR),
                  (int) (rgb[1] * MAX_WCOLOR),
                  (int) (rgb[2] * MAX_WCOLOR));
    }
 
Example 12
protected void fillRect(float x, float y, float width, float height,
                        Color color) {
    float[] rgb = color.getRGBColorComponents(null);

    fillRect(getPrintDC(), x, y, width, height,
             (int) (rgb[0] * MAX_WCOLOR),
             (int) (rgb[1] * MAX_WCOLOR),
             (int) (rgb[2] * MAX_WCOLOR));
}
 
Example 13
protected void fillRect(float x, float y, float width, float height,
                        Color color) {
    float[] rgb = color.getRGBColorComponents(null);

    fillRect(getPrintDC(), x, y, width, height,
             (int) (rgb[0] * MAX_WCOLOR),
             (int) (rgb[1] * MAX_WCOLOR),
             (int) (rgb[2] * MAX_WCOLOR));
}
 
Example 14
Source Project: pdfxtk   File: Displayable.java    License: Apache License 2.0 5 votes vote down vote up
protected Color getHilightColor(Color rgbColor) {
  Color h = (Color) hilightColors.get(rgbColor);
  if (h == null) {
    float[] rgb = rgbColor.getRGBColorComponents(null);
    float[] hsb = Color.RGBtoHSB((int) rgb[0], (int) rgb[1], (int) rgb[2], null);
    h = Color.getHSBColor(hsb[0]+0.5F, hsb[1], Math.max(1.0F, hsb[2]*2));
    hilightColors.put(rgbColor, h);
  }
  return h;
}
 
Example 15
protected boolean selectStylePen(int cap, int join, float width,
                                 Color color) {

    long endCap;
    long lineJoin;

    float[] rgb = color.getRGBColorComponents(null);

    switch(cap) {
    case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
    case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
    default:
    case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
    }

    switch(join) {
    case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
    default:
    case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
    case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
    }

    return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
                           (int) (rgb[0] * MAX_WCOLOR),
                           (int) (rgb[1] * MAX_WCOLOR),
                           (int) (rgb[2] * MAX_WCOLOR)));
}
 
Example 16
private static boolean compareWithTolerance(Color a, Color b, float delta) {
    float[] a_rgb = new float[3];
    a_rgb = a.getRGBColorComponents(a_rgb);
    float[] b_rgb = new float[3];
    b_rgb = b.getRGBColorComponents(b_rgb);

    for (int i = 0; i < 3; i++) {
        if (Math.abs(a_rgb[i] - b_rgb[i]) > delta) {
            return false;
        }
    }
    return true;
}
 
Example 17
protected void fillRect(float x, float y, float width, float height,
                        Color color) {
    float[] rgb = color.getRGBColorComponents(null);

    fillRect(getPrintDC(), x, y, width, height,
             (int) (rgb[0] * MAX_WCOLOR),
             (int) (rgb[1] * MAX_WCOLOR),
             (int) (rgb[2] * MAX_WCOLOR));
}
 
Example 18
Source Project: hottub   File: WPrinterJob.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected void selectPen(float width, Color color) {

        float[] rgb = color.getRGBColorComponents(null);

        selectPen(getPrintDC(), width,
                  (int) (rgb[0] * MAX_WCOLOR),
                  (int) (rgb[1] * MAX_WCOLOR),
                  (int) (rgb[2] * MAX_WCOLOR));
    }
 
Example 19
protected boolean selectStylePen(int cap, int join, float width,
                                 Color color) {

    long endCap;
    long lineJoin;

    float[] rgb = color.getRGBColorComponents(null);

    switch(cap) {
    case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
    case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
    default:
    case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
    }

    switch(join) {
    case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
    default:
    case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
    case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
    }

    return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
                           (int) (rgb[0] * MAX_WCOLOR),
                           (int) (rgb[1] * MAX_WCOLOR),
                           (int) (rgb[2] * MAX_WCOLOR)));
}
 
Example 20
Source Project: webcurator   File: CreateFlagController.java    License: Apache License 2.0 4 votes vote down vote up
/**
 *  Convert a RGB Color to it corresponding HSL values.
 *
 *  @return an array containing the 3 HSL values.
 */
public static float[] fromRGB(Color color)
{
	//  Get RGB values in the range 0 - 1

	float[] rgb = color.getRGBColorComponents( null );
	float r = rgb[0];
	float g = rgb[1];
	float b = rgb[2];

	//	Minimum and Maximum RGB values are used in the HSL calculations

	float min = Math.min(r, Math.min(g, b));
	float max = Math.max(r, Math.max(g, b));

	//  Calculate the Hue

	float h = 0;

	if (max == min)
		h = 0;
	else if (max == r)
		h = ((60 * (g - b) / (max - min)) + 360) % 360;
	else if (max == g)
		h = (60 * (b - r) / (max - min)) + 120;
	else if (max == b)
		h = (60 * (r - g) / (max - min)) + 240;

	//  Calculate the Luminance

	float l = (max + min) / 2;
	//System.out.println(max + " : " + min + " : " + l);

	//  Calculate the Saturation

	float s = 0;

	if (max == min)
		s = 0;
	else if (l <= .5f)
		s = (max - min) / (max + min);
	else
		s = (max - min) / (2 - max - min);

	return new float[] {h, s * 100, l * 100};
}