stupid 'Color' or stupid me?!

hi guys, i need your help once more!

The aim of the game is to modify the color of pixels in an image… More precisely, to change the hue and the saturation to custom values.
I found no simple way to achieve this, the only one is the horrible code following!


Color pixelColor;
float[] hsb = new int[3];
int alpha;

for (int x=0; x<width; x++)
for (int y=0; y<height; y++)
{
   // Take pixel color information.
   int rgb = image.getRGB(x,y);
   pixelColor = new Color(rgb);
   // get alpha
   alpha = pixelColor.getAlpha();      
   // get brightness (is put in hsb[2])
   Color.RGBtoHSB(
       pixelColor.getRed(),
       pixelColor.getGreen(),
       pixelColor.getBlue(),
       hsb);
      
   // Create the new color
   pixelColor = Color.getHSBColor(myHue, mySaturation, hsb[2]);
   // Create the new color again with the rigth alpha
   pixelColor = new Color(
        pixelColor.getRed(),
        pixelColor.getGreen(),
        pixelColor.getBlue(),
        alpha);
      
   // Get it back again in RGB code
   rgb = pixelColor.getRGB();
   // Change the pixel's color
   image.setRGB(rgb);
}

Isn’t there something simplier to do it?!?!

Have a look at BufferedImage.getData() it returns a Raster object from which you can get the image data. At that point you should be able to manipulate the pixel data.

sorry, it doesn’t help me. I’ve still no clue how i should transform the data. ???
What i should find is the algorithm to perform:

getHue(argb)
getSat(argb)
getBright(argb)
getAlpha(argb)
int getARGB (hue, sat, bright, alpha)

(note: the value is of type TYPE_INT_ARGB if it can help you)
I’ll continue to search tommorow, but if you have any links to point me at, they’re welcome!
thanks

Guess I don’t understand what you’re looking for, here is the code from the Color class.


    /**
     * Converts the components of a color, as specified by the default RGB 
     * model, to an equivalent set of values for hue, saturation, and 
     * brightness that are the three components of the HSB model. 
     * <p>
     * If the <code>hsbvals</code> argument is <code>null</code>, then a 
     * new array is allocated to return the result. Otherwise, the method 
     * returns the array <code>hsbvals</code>, with the values put into 
     * that array. 
     * @param     r   the red component of the color
     * @param     g   the green component of the color
     * @param     b   the blue component of the color
     * @param     hsbvals  the array used to return the 
     *                     three HSB values, or <code>null</code>
     * @return    an array of three elements containing the hue, saturation, 
     *                     and brightness (in that order), of the color with 
     *                     the indicated red, green, and blue components.
     * @see       java.awt.Color#getRGB()
     * @see       java.awt.Color#Color(int)
     * @see       java.awt.image.ColorModel#getRGBdefault()
     * @since     JDK1.0
     */
    public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
      float hue, saturation, brightness;
      if (hsbvals == null) {
          hsbvals = new float[3];
      }
          int cmax = (r > g) ? r : g;
      if (b > cmax) cmax = b;
      int cmin = (r < g) ? r : g;
      if (b < cmin) cmin = b;

      brightness = ((float) cmax) / 255.0f;
      if (cmax != 0)
          saturation = ((float) (cmax - cmin)) / ((float) cmax);
      else
          saturation = 0;
      if (saturation == 0)
          hue = 0;
      else {
          float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
          float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
          float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
          if (r == cmax)
            hue = bluec - greenc;
          else if (g == cmax)
              hue = 2.0f + redc - bluec;
            else
            hue = 4.0f + greenc - redc;
          hue = hue / 6.0f;
          if (hue < 0)
            hue = hue + 1.0f;
      }
      hsbvals[0] = hue;
      hsbvals[1] = saturation;
      hsbvals[2] = brightness;
      return hsbvals;
    }


well, i guess i can’t simplify without difficulties…
thanks anyway