Get Color using integer - java

The title is a bit confusing but I want to add an integer to a color method. I mean if I had three Colors and want to go through them with a for loop (Something like that below):
Color color1 = something;
Color color2 = something;
Color color3 = something;
for (int i = 1; i < 4; i++) {
int r = color(i).getRed();
int g = color(i).getGreen();
int b = color(i).getBlue();
}

You can store the three colors in an array and access that array inside your loop.
// take these three colors for example
Color[] colors = {Color.BLACK, Color.WHITE, Color.YELLOW};
for (int i=0; i<3; i++) {
int r = colors[i].getRed();
int g = colors[i].getGreen();
int b = colors[i].getBlue();
}

Related

Cannot find symbol - method setColor(java.awt.Color)?

I am attempting to create a new color palette for an image turned to gray scale, and then apply the palette to the gray scale image. I began the method that I wanted to use to apply the palette, but I ran into the error mentioned in the title. I used "java.awt.Color" already in my code, so I am not sure why I am getting the error. Also, as you will see, I placed a color inside the parenthesis.
/**
* This program takes an image, converts it to grayscale, and uses a color palette to create new colors for the image.
*
* #author Dylan Hubbs
* #version 08/02/16
*/
import java.awt.Color ;
class ColorPalette
{
public void grayscaleEffect(Picture pictureObj)
{
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel grayscaleTargetPixel = new Pixel(pictureObj, 0,0);
Color grayscalePixelColor = null;
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
grayscaleTargetPixel = pictureObj.getPixel(x,y);
grayscalePixelColor = grayscaleTargetPixel.getColor(); //gets the color of the target pixel
grayscalePixelColor = new Color((grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3, (grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3, (grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3);
grayscaleTargetPixel.setColor(grayscalePixelColor); //sets the new color of the target pixel
}//end of the inner for loop
}//end of the outer for loop
pictureObj.explore(); //explore the Picture object which is now the altered image
pictureObj.write("grayscaleWashingtonMonument.jpg"); //write the altered Picture object to a new file
pictureObj.show();
}
public void paletteEffect(Picture pictureObj)
{
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel paletteTargetPixel = new Pixel(pictureObj, 0,0);
Color palettePixelColor = null;
Color [] palette = {Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.GRAY, Color.PINK, Color.ORANGE};
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
paletteTargetPixel = pictureObj.getPixel(x,y);
palettePixelColor = paletteTargetPixel.getColor();
if(paletteTargetPixel.getRed() >= 1 && paletteTargetPixel.getRed() <= 31)
palettePixelColor.setColor(palette[0]);
else if(paletteTargetPixel.getRed() >= 32 && paletteTargetPixel.getRed() <= 62)
palettePixelColor.setColor(palette[1]);
else if(paletteTargetPixel.retRed() >= 63 && paletteTargetPixel.getRed() <=93)
palettePixelColor.setColor(palette[2]);
}
}
}
}
public class ColorPaletteTester
{
public static void main(String[] args)
{
Picture pictureObj = new Picture("washingtonmonument.jpg"); //creates a new Picture object representing the file in the parameter list
pictureObj.explore();
ColorPalette cp = new ColorPalette();
cp.grayscaleEffect(pictureObj);
cp.paletteEffect(pictureObj);
}
}
So, the error is coming at
palettePixelColor.setColor(palette[0]);
Does anyone know why this would be happening?
palettePixelColor is declared as java.awt.Color, which happens to be an immutable class with no setters. Depending on what Pixel is, it may have such a method.
You are probably trying to do something like
palettePixelColor = palette[0];
or
paletteTargetPixel.setColor(palette[0]);

Getting the color of a pixel [duplicate]

Given an image file, say of PNG format, how to I get an array of int [r,g,b,a] representing the pixel located at row i, column j?
So far I am starting here:
private static int[][][] getPixels(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
int[][][] result = new int[height][width][4];
// SOLUTION GOES HERE....
}
Thanks in advance!
You need to get the packed pixel value as an int, you can then use Color(int, boolean) to build a color object from which you can extract the RGBA values, for example...
private static int[][][] getPixels(BufferedImage image) {
int[][][] result = new int[height][width][4];
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color c = new Color(image.getRGB(i, j), true);
result[y][x][0] = c.getRed();
result[y][x][1] = c.getGreen();
result[y][x][2] = c.getBlue();
result[y][x][3] = c.getAlpha();
}
}
}
It's not the most efficient method, but it is one of the simplest
BufferedImages have a method called getRGB(int x, int y) which returns an int where each byte is the components of the pixel (alpha, red, green and blue). If you dont want to do the bitwise operators yourself you can use Colors.getRed/Green/Blue methods by creating a new instance of Java.awt.Color with the int from getRGB.
You can do this in a loop to fill the three-dimensional array.
This is my code for this problem:
File f = new File(filePath);//image path with image name like "lena.jpg"
img = ImageIO.read(f);
if (img==null) //if img null return
return;
//3d array [x][y][a,r,g,b]
int [][][]pixel3DArray= new int[img.getWidth()][img.getHeight()][4];
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int px = img.getRGB(x,y); //get pixel on x,y location
//get alpha;
pixel3DArray[x][y][0] =(px >> 24)& 0xff; //shift number and mask
//get red
pixel3DArray[x][y][1] =(px >> 16)& 0xff;
//get green
pixel3DArray[x][y][2] =(px >> 8)& 0xff;
//get blue
pixel3DArray[x][y][3] =(px >> 0)& 0xff;
}
}

assign RGB values in java with nested for function

I have some question, here is my code :
int W = img.getWidth();
int H = img.getHeight();
int [][] pixels = new int [W][H];
int [][][] rgb = new int [3][H][W];
for(int i=0;i<W;i++)
for(int j=0;j<H;j++){
pixels[i][j] = img.getRGB(i,j);
Color clr = new Color(pixels[i][j]);
rgb[0][j][i] = clr.getRed();
rgb[1][j][i] = clr.getGreen();
rgb[2][j][i] = clr.getBlue();
}
/*
pixels changing process
*/
//1st for
for(int[] asd : rgb[0])
System.out.println(Arrays.toString(asd));
//2nd for
for(int i=0;i<W;i++)
for(int j=0;j<H;j++){
/*Color myColor = new Color (rgb[0][i][j],rgb[1][i][j],rgb[2][i][j]);
int newrgb = myColor.getRGB();
img.setRGB(W,H,newrgb);*/
}
}
printing the red values with 1st works normally, but why I can't put that values using 2nd for ?
when I run the code, it issues ByteInterleavedRaster.setDataElements(int, int, Object) line: not available
I want to assign image colors with new values of rgb[0] (red), rgb[1] (green), rgb[2] (blue) that printed by using 1st for. When I expected it could work with 2nd for, it threw an error.
thanks in advance :)
I think this looks correct
int W = img.getWidth();
int H = img.getHeight();
int [][] pixels = new int [W][H];
int [][][] rgb = new int [3][W][H];
for(int i=0; i<W; i++)
for(int j=0; j<H; j++) {
pixels[i][j] = img.getRGB(i,j);
Color clr = new Color(pixels[i][j]);
rgb[0][i][j] = clr.getRed();
rgb[1][i][j] = clr.getGreen();
rgb[2][i][j] = clr.getBlue();
}
/*
pixels changing process
*/
for(int i=0; i<W; i++)
for(int j=0; j<H; j++){
Color myColor = new Color (rgb[0][i][j],
rgb[1][i][j],
rgb[2][i][j]);
int newrgb = myColor.getRGB();
img.setRGB(i, j, newrgb);
}
}
You have to change this line img.setRGB(W,H,newrgb) to this one img.setRGB(j,i,newrgb).

Manipulate pixel colors to create a sunset

I have to create a program that manipulates the color pixels of an image to create a look of a sunset in the background. I know to do this I have to increase the amount of the color red in the picture but whenever I program it it results in an error saying:
java.lang.IllegalArgumentException:
Color parameter outside of expected range: Red(in java.awt.Color)
My program looks like this:
import java.awt.*;
public class TrueColors2
{
public static void main(String[] args)
{
Picture pictureObj = new Picture("WashingtonMonument.png");
//picutreObj.explore();
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel targetPixel = new Pixel(pictureObj, 0, 0);
Color pixelColor = null;
for(int y = 0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
targetPixel = pictureObj.getPixel(x,y);
pixelColor = targetPixel.getColor();
redValue = pixelColor.getRed();
greenValue = pixelColor.getGreen();
blueValue = pixelColor.getBlue();
pixelColor = new Color(redValue, greenValue, blueValue);
pixelColor = new Color(100 + pixelColor.getRed(),
pixelColor.getGreen(),
pixelColor.getBlue());
targetPixel.setColor(pixelColor);
}
}
pictureObj.write("ThirdWashingtonMonument.png");
pictureObj.show();
}
}
Can someone help me figure out what is wrong and help me figure out how to create an effect of a sunset?
Thanks
When you add 100 to your red value, you're probably exceeding the maximum value of 255 at some point. You should consider inspecting the current value of pixelColor.getRed() before you add 100 to it.

How to dynamically compute a list of colors?

In order to represent a List of Objects with different colors in a GWT-Widget, we need to get dynamically a List of colors with as much different colors as objects. Since the size of the List can vary, we need to be able to compute such a List of colors.
Another version of my solution with ranges:
List<int> getUniqueColors(int amount) {
final int lowerLimit = 0x10;
final int upperLimit = 0xE0;
final int colorStep = (upperLimit-lowerLimit)/Math.pow(amount,1f/3);
final List<int> colors = new ArrayList<int>(amount);
for (int R = lowerLimit;R < upperLimit; R+=colorStep)
for (int G = lowerLimit;G < upperLimit; G+=colorStep)
for (int B = lowerLimit;B < upperLimit; B+=colorStep) {
if (colors.size() >= amount) { //The calculated step is not very precise, so this safeguard is appropriate
return colors;
} else {
int color = (R<<16)+(G<<8)+(B);
colors.add(color);
}
}
return colors;
}
This one is more advance as it generates the colors that differ from each other as much as possible (something like #aiiobe did).
Generally we split the range to 3 subranges of red green and blue, calculate how many steps do we need to iterate each of them (by applying a pow(range,1f/3)) and iterate them.
Given the number 3 for example, it will generate 0x0000B1, 0x00B100, 0x00B1B1. For number 10 it will be: 0x000076, 0x0000EC, 0x007600, 0x007676, 0x0076EC, 0x00EC00, 0x00EC76, 0x00ECEC, 0x760000, 0x760076
Something like this would do I guess. No randomness, just calculates which color steps to take and splits all color range to that steps. If you limit the lower limit - you will remove too dark colors, and limiting the upper limit will remove too bright colors.
List<Integer> getUniqueColors(int amount) {
final int lowerLimit = 0x101010;
final int upperLimit = 0xE0E0E0;
final int colorStep = (upperLimit-lowerLimit)/amount;
final List<Integer> colors = new ArrayList<Integer>(amount);
for (int i=0;i<amount;i++) {
int color = lowerLimit+colorStep*i;
colors.add(color);
}
return colors;
}
If I understand your situation correct, you're after a number of colors that look sort of "as different as possible"? I would in that case suggest that you vary the hue value (two red colors with slightly different brightness won't look much different), so you get something like a "rainbow-palette":
This can be achieved with the following code:
Color[] cols = new Color[n];
for (int i = 0; i < n; i++)
cols[i] = Color.getHSBColor((float) i / n, 1, 1);
An example usage with a screen shots below:
import java.awt.*;
public class TestComponent extends JPanel {
int numCols = 6;
public void paint(Graphics g) {
float h = 0, dh = (float) getHeight() / numCols;
Color[] cols = getDifferentColors(numCols);
for (int i = 0; i < numCols; i++) {
g.setColor(cols[i]);
g.fillRect(0, (int) h, getWidth(), (int) (h += dh));
}
}
public static Color[] getDifferentColors(int n) {
Color[] cols = new Color[n];
for (int i = 0; i < n; i++)
cols[i] = Color.getHSBColor((float) i / n, 1, 1);
return cols;
}
public static void main(String s[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TestComponent());
f.setSize(200, 200);
f.setVisible(true);
}
}
numCols = 6 and numCols = 40 yields the following two screenshots:
If you need like more than 30 colors, you could of course change the brightness and perhaps the saturation as well, and have, for instance, 10 dark colors, 10 midtone colors, and 10 bright colors.

Categories

Resources