rgb - How to create X% percent gray color in Java? -
Suppose i want gray color 25% or 31% in java?
The following shows the code
BufferedImage image = new BufferedImage (2, 2, BufferedImage.TYPE_BYTE_GRAY); Image.setRGB (0, 0, new color (0,0,0) .getRGB ()); Image.setRGB (1, 0, new color (50, 50, 50) .getRGB ()); Image.setRGB (0, 1, new color (100,100,100) .getRGB ()); Image.setRGB (1, 1, new color (255,255,255) .getRGB ()); Raster raster = image.getData (); Double [] data = raster.gate pixel (0, 0, raster .getWidth (), rastergatehit (), (double []) empty); Println (Arrays.toString (data));
The obvious fact, that RGC density (?) Is related to non-linear
[0.0, 8.0, 32.0, 255.0]
So, how can the color of given density be made?
UPDATE
I have tried, the methods proposed by @Inza and HLLG and I also found another one:
< Code> double [] data; Raster raster; BufferedImage image = new BufferedImage (1, 1, BufferedImage.TYPE_BYTE_GRAY); Float [] Grayz = {0, 0.25F, 0.5F, 0.75F, 1}; ColorSpace Linear RGB = Rangespace Get Instances (ColorSpace. CS_LANG ARR); ColorSpace Gray = Rangespace.tastens (colorspace.cs_grae); Color color; Int [] RGB; For (int i = 0; i & lt; grays.length; ++ i) {System.out.println ("should \ n \ n" + (grays [i] * 100) + "% gray") ; Color = new color (linear RGB, new float [] {grays [i], grays [i], grays [i]}, 1f); Image.setRGB (0, 0, color.getRGB ()); Raster = image.getData (); Data = rastergate pixel (0, 0, 1, 1, (double []) empty); System.out.println ("Data CS_LINEAR_RGB (hlg method) =" + Arrays.toString (data)); Color = new color (gray, new float [] {grays [i]}, 1f); Image.setRGB (0, 0, color.getRGB ()); Raster = image.getData (); Data = rastergate pixel (0, 0, 1, 1, (double []) empty); Data from system.out.println ("CS_GRAY =" + Arrays.toString (data)); RGB = Miller RBGB (around the monastery (grass [i] * 255)); Color = new color (RGB [0], RGB [1], RGB [2]); Image.setRGB (0, 0, color.getRGB ()); Raster = image.getData (); Data = rastergate pixel (0, 0, 1, 1, (double []) empty); System.out.println ("data by ICAZA method =" + Arrays.toString (data)); }
And all gave different results!
CS_LINEAR_RGB (should be% gray data 0.0 by HLG method) = [0.0] data CS_GRAY = [0.0] Aisiaajha method = [0.0] data CS_LINEAR_RGB (HLG method) = 25.0% gray data must = [63.0] data CS_GRAY = [64.0] Aisiaafhaid = [36.0] data should be 50.0% gray data Sis_lainaararbi (HLL) Sis_jiarawai = [127.0] data = ISCA method = [128.0] data = Sis_aielaara = [1 0]] by cs_lineArrb (HLG method) = [190.0] of data 75.0% should be gray data [154.0] CS_LINEAR_RGB (HLG method should be 100.0% gray data) = [254.0] data CS_GRAY = [254.0] Aisiaajha method = [255.0]
By Data Now I wonder which is okay?
UPDATE 2
Sorry, should be gray / white percentage, of course, vice versa.
The big difference is due to gamma encoding in sRGB (). SRGB is the default color space used in the color
constructor. If you set your colors using a linear RGB color space, then the gray values are not distorted:
color touch liner RGB = colorspace.tastin (colorspace.cs_lineArr); Color grey50 = new color (linear RGB, new float [] {50f / 255,50f / 255,50f / 255}, 1f); Color grey100 = new color (linear RGB, new float [] (100 F / 255 100 F / 255 100 F / 255}, 1 F) Color grey255 = new color (linear RGB, new float [] (1f, 1f, 1f} , 1f);
However, Color.getRGB
and ImageBuffer.setRGB
while pixel set using linear gray scale value SRGB and back. Thus they are gamma encoded and decoded, generate spherical errors based on the location of the chosen colors.
These errors Writing raw pixel data behind the gray scale color model can be avoided:
Writable Writer Writable = image.getRaster (); Writable.setPixel (0,0, New Int [ ] {64});
Note, you have to complete percentage values, for example, 25% you can not consolidate 63.75
. If you need more accuracy, use TYPE_USHORT_GRAY
instead of TYPE_BYTE_GRAY
.
Comments
Post a Comment