Java Code Examples for java.awt.Color#RGBtoHSB

The following examples show how to use java.awt.Color#RGBtoHSB . 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
private static void test(Color[] colors) {
    JLabel label = new JLabel("Preview Panel"); // NON-NLS: simple label

    JColorChooser chooser = new JColorChooser();
    chooser.setPreviewPanel(label);

    float[] hsb = new float[3];
    for (int i = 0; i < colors.length; i++) {
        Color color = colors[i];
        // Make sure sure that there wasn't a regression
        // in java.awt.Color and the conversion methods
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
        if (!color.equals(Color.getHSBColor(hsb[0], hsb[1], hsb[2]))) {
            throw new Error("color conversion is failed");
        }
        // 4193384 regression test
        if (!color.equals(new JColorChooser(color).getColor())) {
            throw new Error("constructor sets incorrect initial color");
        }
        // 4200976 regression test
        chooser.setColor(color);
        if (!color.equals(label.getForeground())) {
            throw new Error("a custom preview panel doesn't handle colors");
        }
    }
}
 
Example 2
public static FilterAction getHueChannelFA() {
    var rgbOp = new RGBPixelOp() {
        private float[] tmpHSBArray = {0.0f, 0.0f, 0.0f};

        @Override
        public int changeRGB(int a, int r, int g, int b) {
            tmpHSBArray = Color.RGBtoHSB(r, g, b, tmpHSBArray);

            // Color.RGBtoHSB return all values in the 0..1 interval
            int hue = (int) (tmpHSBArray[0] * 255);

            r = hue;
            g = hue;
            b = hue;

            return a << 24 | r << 16 | g << 8 | b;
        }
    };
    return rgbOp.toFilterAction("Hue");
}
 
Example 3
Source Project: pdfxtk   File: Style.java    License: Apache License 2.0 6 votes vote down vote up
/**
   * Calculates hilight colors.
   *
   * @param rgbColor Base color
   * @return "hilighted" version of rgbColor
   */

  Color getHilightColor(Color rgbColor) {
    Color h = (Color) hilightColors.get(rgbColor);
    if (h == null) {
      float[] rgb = rgbColor.getRGBColorComponents(null);
      // Some implementations return values between 0 and 1, others between 0 and 255...
//       if (rgb[0] <= 1.0 && rgb[1] <= 1.0 && rgb[2] < 1.0) {
// 	rgb[0] = rgb[0]*256;
// 	rgb[1] = rgb[1]*256;
// 	rgb[2] = rgb[2]*256;
//       }
      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 4
private Point closestPointToColor(Color target)
{
	float[] hsb = Color.RGBtoHSB(target.getRed(), target.getGreen(), target.getBlue(), null);
	int offSize = size - 1;

	return new Point((int) (hsb[1] * offSize), offSize - (int) (hsb[2] * offSize));
}
 
Example 5
Source Project: seaglass   File: AbstractRegionPainter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new color with the saturation cut to one third the original,
 * and the brightness moved one third closer to white.
 *
 * @param  color the original color.
 *
 * @return the new color.
 */
protected Color desaturate(Color color) {
    float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);

    tmp[1] /= 3.0f;
    tmp[2] = clamp(1.0f - (1.0f - tmp[2]) / 3f);

    return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF));
}
 
Example 6
public static short getNearestColor( final Color awtColor, final Map triplets ) {
  if ( awtColor == null ) {
    throw new NullPointerException();
  }

  if ( triplets == null || triplets.isEmpty() ) {
    logger.warn( "Unable to get triplet hashtable" );
    return HSSFColor.BLACK.index;
  }

  short color = HSSFColor.BLACK.index;
  double minDiff = Double.MAX_VALUE;

  // get the color without the alpha chanel
  final float[] hsb = Color.RGBtoHSB( awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue(), null );

  float[] excelHsb = null;
  final Iterator elements = triplets.values().iterator();
  while ( elements.hasNext() ) {
    final HSSFColor crtColor = (HSSFColor) elements.next();
    final short[] rgb = crtColor.getTriplet();
    excelHsb = Color.RGBtoHSB( rgb[0], rgb[1], rgb[2], excelHsb );

    final double weight =
        3.0 * ( Math.min( Math.abs( excelHsb[0] - hsb[0] ), Math.abs( excelHsb[0] - hsb[0] + 1 ) ) )
            + Math.abs( excelHsb[1] - hsb[1] ) + Math.abs( excelHsb[2] - hsb[2] );

    if ( weight < minDiff ) {
      minDiff = weight;
      if ( minDiff == 0 ) {
        // we found the color ...
        return crtColor.getIndex();
      }
      color = crtColor.getIndex();
    }
  }
  return color;
}
 
Example 7
Source Project: ghidra   File: EntropyFieldFactory.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.app.util.viewer.field.FieldFactory#getField(ProxyObj, int)
 */
@Override
public ListingField getField(ProxyObj<?> proxy, int varWidth) {
	if (!enabled) {
		return null;
	}

	Object obj = proxy.getObject();
	if (!enabled || !(obj instanceof CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) obj;

	byte[] bytes = null;
	double entropy = 0.0;
	try {
		bytes = new byte[256];
		int num = cu.getProgram().getMemory().getBytes(cu.getAddress(), bytes);
		if (num < bytes.length) {
			return null;
		}
		entropy = calcEntropy(bytes, 0, bytes.length);
		float[] hsbvals = Color.RGBtoHSB(255, 0, 0, null);
		color =
			Color.getHSBColor(hsbvals[0], hsbvals[1], (float) (hsbvals[1] * (entropy / 8.0)));
	}
	catch (MemoryAccessException e) {
		return null;
	}

	String str = "" + (int) ((entropy / 8.0) * 100);
	AttributedString text = new AttributedString(str, color, getMetrics());

	FieldElement fieldElement = new TextFieldElement(text, 0, 0);
	return ListingTextField.createSingleLineTextField(this, proxy, fieldElement,
		startX + varWidth, width, hlProvider);
}
 
Example 8
Source Project: OpenRS   File: ModelViewer.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static int rgbToHSB(int red, int green, int blue) {
	float[] HSB = Color.RGBtoHSB(red, green, blue, null);
	float hue = (HSB[0]);
	float saturation = (HSB[1]);
	float brightness = (HSB[2]);
	int encode_hue = (int) (hue * 63); // to 6-bits
	int encode_saturation = (int) (saturation * 7); // to 3-bits
	int encode_brightness = (int) (brightness * 127); // to 7-bits
	return (encode_hue << 10) + (encode_saturation << 7) + (encode_brightness);
}
 
Example 9
/**
 * Apply color and brightness to the given image. This uses the hue and saturation of the given
 * color. It ignores the brightness of the color and uses the given brightness value.
 */
public static void colorize(final Color color, final int brightness, final BufferedImage image) {
  final float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
  colorize((int) (hsb[0] * 360), (int) (hsb[1] * 100), brightness, image);
}
 
Example 10
/** Sets the selected color of this panel.
 * <P>If this panel is in HUE, SAT, or BRI mode, then
 * this method converts these values to HSB coordinates
 * and calls <code>setHSB</code>.
 * <P>This method may regenerate the graphic if necessary.
 * 
 * @param r the red value of the selected color.
 * @param g the green value of the selected color.
 * @param b the blue value of the selected color.
 */
public void setRGB(int r,int g,int b) {
	if(r<0 || r>255)
		throw new IllegalArgumentException("The red value ("+r+") must be between [0,255].");
	if(g<0 || g>255)
		throw new IllegalArgumentException("The green value ("+g+") must be between [0,255].");
	if(b<0 || b>255)
		throw new IllegalArgumentException("The blue value ("+b+") must be between [0,255].");
	
	if(red!=r || green!=g || blue!=b) {
		if(mode==ColorPicker.RED || 
				mode==ColorPicker.GREEN ||
				mode==ColorPicker.BLUE) {
			int lastR = red;
			int lastG = green;
			int lastB = blue;
			red = r;
			green = g;
			blue = b;
			
			if(mode==ColorPicker.RED) {
				if(lastR!=r) {
					regenerateImage();
				}
			} else if(mode==ColorPicker.GREEN) {
				if(lastG!=g) {
					regenerateImage();
				}
			} else if(mode==ColorPicker.BLUE) {
				if(lastB!=b) {
					regenerateImage();
				}
			}
		} else {
			float[] hsb = new float[3];
			Color.RGBtoHSB(r, g, b, hsb);
			setHSB(hsb[0],hsb[1],hsb[2]);
			return;
		}
		regeneratePoint();
		repaint();
		fireChangeListeners();
	}
}
 
Example 11
private static Color brighter(int color)
{
	float[] hsv = new float[3];
	Color.RGBtoHSB(color >>> 16, (color >> 8) & 0xFF, color & 0xFF, hsv);
	return Color.getHSBColor(hsv[0], 1.f, 1.f);
}
 
Example 12
/**
 * Returns the color at the indicated point in HSB values.
 *
 * @param p a point relative to this panel.
 * @return the HSB values at the point provided.
 */
public float[] getHSB(Point p) {
    if (mode == RED || mode == GREEN ||
            mode == BLUE) {
        int[] rgb = getRGB(p);
        float[] hsb = Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], null);
        return hsb;
    }

    int size = Math.min(MAX_SIZE, Math
            .min(getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom));
    p.translate(-(getWidth() / 2 - size / 2), -(getHeight() / 2 - size / 2));
    if (mode == BRI || mode == SAT) {
        //the two circular views:
        double radius = size / 2.0;
        double x = p.getX() - size / 2.0;
        double y = p.getY() - size / 2.0;
        double r = Math.sqrt(x * x + y * y) / radius;
        double theta = Math.atan2(y, x) / (PI * 2.0);

        if (r > 1) {
            r = 1;
        }

        if (mode == BRI) {
            return new float[]{
                    (float) (theta + 0.25f),
                    (float) r,
                    bri};
        } else {
            return new float[]{
                    (float) (theta + 0.25f),
                    sat,
                    (float) r
            };
        }
    } else {
        float s = ((float) p.x) / ((float) size);
        float b = ((float) p.y) / ((float) size);
        if (s < 0) {
            s = 0;
        }
        if (s > 1) {
            s = 1;
        }
        if (b < 0) {
            b = 0;
        }
        if (b > 1) {
            b = 1;
        }
        return new float[]{hue, s, b};
    }
}
 
Example 13
Source Project: ha-bridge   File: ColorDecode.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static String replaceColorData(String request, ColorData colorData, int setIntensity, boolean isHex) {
	if (request == null) {
		return null;
	}
	if (colorData == null) {
		return request;
	}
	boolean notDone = true;
	ColorData.ColorMode colorMode = colorData.getColorMode();
	List<Integer> rgb = null;
	if (colorMode == ColorData.ColorMode.XY) {
		rgb = convertCIEtoRGB((List<Double>) colorData.getData(), setIntensity);
	} else if (colorMode == ColorData.ColorMode.CT) {
		rgb = convertCTtoRGB((Integer) colorData.getData());
	} else if (colorMode == ColorData.ColorMode.HS) {
		rgb = convertHSLtoRGB((HueSatBri) colorData.getData());
	}

	while (notDone) {
		notDone = false;
		if (request.contains(COLOR_R)) {
			request = request.replace(COLOR_R,
					isHex ? String.format("%02X", rgb.get(0)) : String.valueOf(rgb.get(0)));
			notDone = true;
		}

		if (request.contains(COLOR_G)) {
			request = request.replace(COLOR_G,
					isHex ? String.format("%02X", rgb.get(1)) : String.valueOf(rgb.get(1)));
			notDone = true;
		}

		if (request.contains(COLOR_B)) {
			request = request.replace(COLOR_B,
					isHex ? String.format("%02X", rgb.get(2)) : String.valueOf(rgb.get(2)));
			notDone = true;
		}

		if (request.contains(COLOR_RX)) {
			request = request.replace(COLOR_RX, String.format("%02X", rgb.get(0)));
			notDone = true;
		}

		if (request.contains(COLOR_GX)) {
			request = request.replace(COLOR_GX, String.format("%02X", rgb.get(1)));
			notDone = true;
		}

		if (request.contains(COLOR_BX)) {
			request = request.replace(COLOR_BX, String.format("%02X", rgb.get(2)));
			notDone = true;
		}

		if (request.contains(COLOR_RGBX)) {
			request = request.replace(COLOR_RGBX,
					String.format("%02X%02X%02X", rgb.get(0), rgb.get(1), rgb.get(2)));
			notDone = true;
		}

		if (request.contains(COLOR_HSL)) {
			float[] hsb = new float[3];
			Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb);
			float hue = hsb[0] * (float) 360.0;
			float sat = hsb[1] * (float) 100.0;
			float bright = hsb[2] * (float) 100.0;
			request = request.replace(COLOR_HSL, String.format("%f,%f,%f", hue, sat, bright));
			notDone = true;
		}

		Matcher m = COLOR_MILIGHT.matcher(request);
		while (m.find()) {
			int group = Integer.parseInt(m.group(1));
			request = m.replaceFirst(getMilightV5FromRgb(rgb, group));
			m.reset(request);
		}

		log.debug("Request <<" + request + ">>, not done: " + notDone);
	}
	return request;
}
 
Example 14
/**
 * Sets the selected color of this panel.
 * <P>If this panel is in HUE, SAT, or BRI mode, then
 * this method converts these values to HSB coordinates
 * and calls <code>setHSB</code>.
 * <P>This method may regenerate the graphic if necessary.
 *
 * @param r the red value of the selected color.
 * @param g the green value of the selected color.
 * @param b the blue value of the selected color.
 */
public void setRGB(int r, int g, int b) {
    if (r < 0 || r > 255) {
        throw new IllegalArgumentException("The red value (" + r + ") must be between [0,255].");
    }
    if (g < 0 || g > 255) {
        throw new IllegalArgumentException("The green value (" + g + ") must be between [0,255].");
    }
    if (b < 0 || b > 255) {
        throw new IllegalArgumentException("The blue value (" + b + ") must be between [0,255].");
    }

    if (red != r || green != g || blue != b) {
        if (mode == RED ||
                mode == GREEN ||
                mode == BLUE) {
            int lastR = red;
            int lastG = green;
            int lastB = blue;
            red = r;
            green = g;
            blue = b;

            if (mode == RED) {
                if (lastR != r) {
                    regenerateImage();
                }
            } else if (mode == GREEN) {
                if (lastG != g) {
                    regenerateImage();
                }
            } else if (mode == BLUE) {
                if (lastB != b) {
                    regenerateImage();
                }
            }
        } else {
            float[] hsb = new float[3];
            Color.RGBtoHSB(r, g, b, hsb);
            setHSB(hsb[0], hsb[1], hsb[2]);
            return;
        }
        regeneratePoint();
        repaint();
        fireChangeListeners();
    }
}
 
Example 15
Source Project: netbeans   File: DarkIconFilter.java    License: Apache License 2.0 4 votes vote down vote up
private int[] invertHueBrighten(int[] rgb, float brighten) {
    float hsb[] = new float[3];
    Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], hsb);
    return decode(Color.HSBtoRGB(hsb[0] > 0.5f ? hsb[0]-0.5f : hsb[0]+0.5f, hsb[1], hsb[2]+(1.0f-hsb[2])*brighten));
}
 
Example 16
Source Project: MyBox   File: ImageColor.java    License: Apache License 2.0 4 votes vote down vote up
public static Color scaleSaturate(Color color, float scale) {
    float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
    return Color.getHSBColor(hsb[0], hsb[1] * scale, hsb[2]);
}
 
Example 17
@Override
public int apply(int color) {
	float[] hsb = Color.RGBtoHSB((color >> 16) & 255, (color >> 8) & 255, color & 255, null);
	
	int idx = mode.getIdx();
	
	hsb[idx] = level*value + (1-level)*hsb[idx];
	
	return Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
}
 
Example 18
Source Project: GraphicCR   File: GraphicC3Translator.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 目标像素判断
 * <br />(基于饱和度)
 * 
 * @param colorInt
 * @return
 */
private boolean isTarget(int colorInt) {
    Color color = new Color(colorInt);
    float[] hsb = new float[3];
    Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
    return hsb[1] > 0.2f; // 饱和度
}
 
Example 19
Source Project: GraphicCR   File: GraphicC7Translator.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 目标像素判断
 * <br />(基于亮度)
 * 
 * @param colorInt
 * @return
 */
private boolean isTarget(int colorInt) {
    Color color = new Color(colorInt);
    float[] hsb = new float[3];
    Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
    return hsb[2] < 0.5f; // 亮度
}
 
Example 20
Source Project: GraphicCR   File: GraphicC5Translator.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 目标像素判断
 * <br />(基于亮度)
 * 
 * @param colorInt
 * @return
 */
private boolean isTarget(int colorInt) {
    Color color = new Color(colorInt);
    float[] hsb = new float[3];
    Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
    return hsb[2] < 0.5f; // 亮度
}