2013-12-12 70 views
0

這是我用來更改圖像的色調,飽和度和亮度的當前代碼。我怎麼去只改變飽和度或亮度而不添加色調?或者我如何找到像素的當前色相值,以便我可以使用它?只更改一個值HSL

public static final int MAX_COLOR = 256; 

    private final float LUMINANCE_RED = 0.2126f; 
    private final float LUMINANCE_GREEN = 0.7152f; 
    private final float LUMINANCE_BLUE = 0.0722f; 

    private double hue = 180; 
    private double saturation = 50; 
    private double lightness = 0; 

    private int[] lum_red_lookup; 
    private int[] lum_green_lookup; 
    private int[] lum_blue_lookup; 

    private int[] final_red_lookup; 
    private int[] final_green_lookup; 
    private int[] final_blue_lookup; 

    public void doHSB(double t_hue, double t_sat, double t_bri, BufferedImage image) 
    { 
     hue = t_hue; 
     saturation = t_sat; 
     lightness = t_bri; 
     doInit(); 
     doColorize(image); 
    } 
    private void doInit() 
    { 
     lum_red_lookup = new int[MAX_COLOR]; 
     lum_green_lookup = new int[MAX_COLOR]; 
     lum_blue_lookup = new int[MAX_COLOR]; 

     double temp_hue = hue/360f; 
     double temp_sat = saturation/100f; 

     final_red_lookup = new int[MAX_COLOR]; 
     final_green_lookup = new int[MAX_COLOR]; 
     final_blue_lookup = new int[MAX_COLOR]; 

     for (int i = 0; i < MAX_COLOR; ++i) 
     { 
      lum_red_lookup[i] = (int) (i * LUMINANCE_RED); 
      lum_green_lookup[i] = (int) (i * LUMINANCE_GREEN); 
      lum_blue_lookup[i] = (int) (i * LUMINANCE_BLUE); 

      double temp_light = (double) i/255f; 

      Color color = new Color(Color.HSBtoRGB((float) temp_hue, (float) temp_sat, (float) temp_light)); 

      final_red_lookup[i] = (int) (color.getRed()); 
      final_green_lookup[i] = (int) (color.getGreen()); 
      final_blue_lookup[i] = (int) (color.getBlue()); 
     } 
    } 

    private void doColorize(BufferedImage image) 
    { 
     int height = image.getHeight(); 
     int width; 

     while (height-- != 0) 
     { 
      width = image.getWidth(); 

      while (width-- != 0) 
      { 
       Color color = new Color(image.getRGB(width, height), true); 

       int lum = lum_red_lookup[color.getRed()] + lum_green_lookup[color.getGreen()] + lum_blue_lookup[color.getBlue()]; 

       if (lightness > 0) 
       { 
        lum = (int) ((double) lum * (100f - lightness)/100f); 
        lum += 255f - (100f - lightness) * 255f/100f; 
       } 
       else if (lightness < 0) 
       { 
        lum = (int) (((double) lum * (lightness + 100f))/100f); 
       } 
       Color final_color = new Color(final_red_lookup[lum], final_green_lookup[lum], final_blue_lookup[lum], color.getAlpha()); 
       image.setRGB(width, height, final_color.getRGB()); 
      } 
     } 
    } 
+0

而不是發佈*所有*您的代碼,你可以只發布相關部分?謝謝! –

+0

已修改。但我拿出的只是構造函數和類聲明。儘可能短,我可以得到它,並保持它的工作。希望有幫助。 – 0circle0

回答