Algorithm for Hue/Saturation Adjustment Layer from Photoshop

后端 未结 6 605
春和景丽
春和景丽 2021-01-31 11:39

Does anyone know how adjustment layers work in Photoshop? I need to generate a result image having a source image and HSL values from Hue/Saturation adjustment layer. Conversion

6条回答
  •  爱一瞬间的悲伤
    2021-01-31 12:01

    I did translate @Roman Starkov solution to java if any one needed, but for some reason It not worked so well, then I started read a little bit and found that the solution is very simple , there are 2 things have to be done :

    1. When changing the hue or saturation replace the original image only hue and saturation and the lightness stay as is was in the original image this blend method called 10.2.4. luminosity blend mode : https://www.w3.org/TR/compositing-1/#backdrop

    2. When changing the lightness in photoshop the slider indicates how much percentage we need to add or subtract to/from the original lightness in order to get to white or black color in HSL.

    for example : If the original pixel is 0.7 lightness and the lightness slider = 20 so we need more 0.3 lightness in order to get to 1

    so we need to add to the original pixel lightness : 0.7 + 0.2*0.3; this will be the new blended lightness value for the new pixel .

    @Roman Starkov solution Java implementation :

    //newHue, which is photoshop_hue (i.e. 0..360)
    //newSaturation, which is photoshop_saturation / 100.0 (i.e. 0..1)
    //newLightness, which is photoshop_lightness / 100.0 (i.e. -1..1)
    
    //returns rgb int array of new color
    private static int[] colorizeSinglePixel(int originlPixel,int newHue,float newSaturation,float newLightness)
    {
        float[] originalPixelHSV = new float[3];
        Color.colorToHSV(originlPixel,originalPixelHSV);
        float originalPixelLightness = originalPixelHSV[2];
    
        float[] hueRGB_HSV = {newHue,100.0f,100.0f};
        int[] hueRGB = {Color.red(Color.HSVToColor(hueRGB_HSV)),Color.green(Color.HSVToColor(hueRGB_HSV)),Color.blue(Color.HSVToColor(hueRGB_HSV))};
    
    
        int color[] = blend2(new int[]{128,128,128},hueRGB,newSaturation);
        int blackColor[] = new int[]{Color.red(Color.BLACK),Color.green(Color.BLACK),Color.blue(Color.BLACK)};
        int whileColor[] = new int[]{Color.red(Color.WHITE),Color.green(Color.WHITE),Color.blue(Color.WHITE)};
    
        if(newLightness <= -1)
        {
            return blackColor;
        }
        else if(newLightness >=1)
        {
            return whileColor;
        }
        else if(newLightness >=0)
        {
            return blend3(blackColor,color,whileColor, (int) (2*(1-newLightness)*(originalPixelLightness-1) + 1));
        }
        else
        {
            return blend3(blackColor,color,whileColor, (int) ((1+newLightness)*(originalPixelLightness) - 1));
        }
    }
    
    private static int[] blend2(int[] left,int[] right,float pos)
    {
        return new int[]{(int) (left[0]*(1-pos)+right[0]*pos),(int) (left[1]*(1-pos)+right[1]*pos),(int) (left[2]*(1-pos)+right[2]*pos)};
    }
    
    private static int[] blend3(int[] left,int[] main,int[] right,int pos)
    {
        if(pos < 0)
        {
            return blend2(left,main,pos+1);
        }
        else if(pos > 0)
        {
            return blend2(main,right,pos);
        }
        else
        {
            return main;
        }
    
    }
    

提交回复
热议问题