public static float[] getHSB(Color color) { float[] rgb = new float[4]; float[] hsb = new float[3]; color.getComponents(rgb); int r = (int) (rgb[0] * 256); int g = (int) (rgb[1] * 256); int b = (int) (rgb[2] * 256); Color.RGBtoHSB(r, g, b, hsb); return hsb; }
public static Color brightnessAdjust(Color original, float brightness) { float[] hsb = getHSB(original); hsb[2] = brightness; if (hsb[2] < 0.0f) hsb[2] = 0.0f; else if (hsb[2] > 1.0f) hsb[2] = 1.0f; int newRGB = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]); return new Color(newRGB); }
public static Color saturationAdjust(Color original, float saturation) { float[] hsb = getHSB(original); hsb[1] = saturation; if (hsb[1] < 0.0f) hsb[1] = 0.0f; else if (hsb[1] > 1.0f) hsb[1] = 1.0f; int newRGB = Color.HSBtoRGB(hsb[0], hsb[1], 1.0f); return new Color(newRGB); }
public static Color relativelyTransparent(Color original, float alpha) { ColorSpace srbg = ICC_ColorSpace.getInstance(ColorSpace.CS_sRGB); double originalAlpha = 1.0 * original.getAlpha() / 255; alpha *= originalAlpha; return new Color(srbg, original.getColorComponents(null), alpha); }
public BufferedImage getBufferedImage(int type, Color c) { BufferedImage image = null; float[] colComp = new float[3]; c.getRGBColorComponents(colComp); double red = (double) colComp[0]; double green = (double) colComp[1]; double blue = (double) colComp[2]; // System.out.println("blue, green, red = "+ blue +", " + green + ", " + red); double x = 0.0; double x2; switch (type) { case ScalarImage.TYPE_BYTE_RANDOM: { int numCol = 256; byte[] bBuf = new byte[numCol * 3]; blue *= 255 * 4.; green *= 255 * 4.; red *= 255 * 4.; double delta = 1.0 / (double) (numCol + 1); int j = 0; for (int i = 0; i < numCol; i++) { if (i % 5 == 0) x = 0.7 * Math.random() + 0.3 * x; x2 = x * x; bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x); bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x); bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x); // x += delta; } IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false); // image = new // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm); byte[] idxBuffer = new byte[size]; for (int i = 0; i < size; i++) { idxBuffer[i] = (byte) (clamp(f[i] * 255.)); } DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size); int idxOffset[] = {0}; int idxBits[] = {8}; try { ComponentSampleModel idxSampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset); WritableRaster rasterIdx = java.awt.image.Raster.createWritableRaster( idxSampleModel, dataBuffer, new Point(0, 0)); image = new BufferedImage(cm, rasterIdx, false, null); } catch (Exception e) { System.out.println("Exception caught while acquiring image:"); System.out.println(e.getMessage()); } } break; case BufferedImage.TYPE_BYTE_INDEXED: { int numCol = 256; byte[] bBuf = new byte[numCol * 3]; blue *= 255 * 4.; green *= 255 * 4.; red *= 255 * 4.; double delta = 1.0 / (double) (numCol + 1); int j = 0; for (int i = 0; i < numCol; i++) { x2 = x * x; bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x); bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x); bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x); x += delta; } IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false); // image = new // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm); byte[] idxBuffer = new byte[size]; for (int i = 0; i < size; i++) { idxBuffer[i] = (byte) (clamp(f[i] * 255.)); } DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size); int idxOffset[] = {0}; int idxBits[] = {8}; try { ComponentSampleModel idxSampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset); WritableRaster rasterIdx = java.awt.image.Raster.createWritableRaster( idxSampleModel, dataBuffer, new Point(0, 0)); image = new BufferedImage(cm, rasterIdx, false, null); } catch (Exception e) { System.out.println("Exception caught while acquiring image:"); System.out.println(e.getMessage()); } } break; case BufferedImage.TYPE_BYTE_GRAY: break; case BufferedImage.TYPE_3BYTE_BGR: default: byte[] byteBuffer = new byte[size * 3]; blue *= 255 * 4.; green *= 255 * 4.; red *= 255 * 4.; int j = 0; for (int i = 0; i < size; i++) { x = f[i]; x2 = x * x; /* byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * red ) * x * x + ( 4 * red - 255 ) * x); byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * green ) * x * x + ( 4 * green - 255 ) * x); byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * blue ) * x * x + ( 4 * blue - 255 ) * x); */ byteBuffer[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x); byteBuffer[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x); byteBuffer[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x); } DataBufferByte dataBuffer = new DataBufferByte(byteBuffer, size * 3); int componentOffset[] = {0, 1, 2}; int componentBits[] = {8, 8, 8}; try { WritableRaster raster = java.awt.image.Raster.createWritableRaster( new PixelInterleavedSampleModel( DataBuffer.TYPE_BYTE, width, height, 3, width * 3, componentOffset), dataBuffer, new Point(0, 0)); image = new BufferedImage( new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), componentBits, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE), raster, false, null); } catch (Exception e) { System.out.println("Exception caught while acquiring image:"); System.out.println(e.getMessage()); } break; } return image; }