Algorithm for Hue/Saturation Adjustment Layer from Photoshop

后端 未结 6 599
春和景丽
春和景丽 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;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-31 12:08

    Hello I wrote colorize shader and my equation is as folows

    inputRGB is the source image which should be in monochrome

    (r+g+b) * 0.333
    

    colorRGB is your destination color
    finalRGB is the result

    pseudo code:

    finalRGB = inputRGB * (colorRGB + inputRGB * 0.5);
    

    I think it's fast and efficient

    0 讨论(0)
  • 2021-01-31 12:12

    When the “Colorize” checkbox is checked, the lightness of the underlying layer is combined with the values of the Hue and Saturation sliders and converted from HSL to RGB according to the equations at https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL . (The Lightness slider just remaps the lightness to a subset of the scale as you can see from watching the histogram; the effect is pretty awful and I don’t see why anyone would ever use it.)

    0 讨论(0)
  • 2021-01-31 12:13

    I have figured out how Lightness works.

    The input parameter brightness b is in [0, 2], Output is c (color channel).

    if(b<1) c = b * c;
    else    c = c + (b-1) * (1-c);
    

    Some tests:

    b = 0  >>>  c = 0  // black
    b = 1  >>>  c = c  // same color
    b = 2  >>>  c = 1  // white
    

    However, if you choose some interval (e.g. Reds instead of Master), Lightness behaves completely differently, more like Saturation.

    0 讨论(0)
  • 2021-01-31 12:16

    Photoshop, dunno. But the theory is usually: The RGB image is converted to HSL/HSV by the particular layer's internal methods; each pixel's HSL is then modified according to the specified parameters, and the so-obtained result is being provided back (for displaying) in RGB.

    PaintShopPro7 used to split up the H space (assuming a range of 0..360) in discrete increments of 30° (IIRC), so if you bumped only the "yellows", i.e. only pixels whose H component was valued 45-75 would be considered for manipulation.

    reds 345..15, oranges 15..45, yellows 45..75, yellowgreen 75..105, greens 105..135, etc.

    if (h >= 45 && h < 75)
            s += s * yellow_percent;
    

    There are alternative possibilities, such as applying a falloff filter, as in:

    /* For h=60, let m=1... and linearly fall off to h=75 m=0. */
    m = 1 - abs(h - 60) / 15;
    if (m < 0)
            m = 0;
    s += s * yellow_percent * d;
    
    0 讨论(0)
  • 2021-01-31 12:20

    I've reverse-engineered the computation for when the "Colorize" checkbox is checked. All of the code below is pseudo-code.

    The inputs are:

    • hueRGB, which is an RGB color for HSV(photoshop_hue, 100, 100).ToRGB()
    • saturation, which is photoshop_saturation / 100.0 (i.e. 0..1)
    • lightness, which is photoshop_lightness / 100.0 (i.e. -1..1)
    • value, which is the pixel.ToHSV().Value, scaled into 0..1 range.

    The method to colorize a single pixel:

    color = blend2(rgb(128, 128, 128), hueRGB, saturation);
    
    if (lightness <= -1)
        return black;
    else if (lightness >= 1)
        return white;
    
    else if (lightness >= 0)
        return blend3(black, color, white, 2 * (1 - lightness) * (value - 1) + 1)
    else
        return blend3(black, color, white, 2 * (1 + lightness) * (value) - 1)
    

    Where blend2 and blend3 are:

    blend2(left, right, pos):
        return rgb(left.R * (1-pos) + right.R * pos, same for green, same for blue)
    
    blend3(left, main, right, pos):
        if (pos < 0)
            return blend2(left, main, pos + 1)
        else if (pos > 0)
            return blend2(main, right, pos)
        else
            return main
    
    0 讨论(0)
提交回复
热议问题