Algorithm for Hue/Saturation Adjustment Layer from Photoshop

后端 未结 6 759
春和景丽
春和景丽 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: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
    

提交回复
热议问题