0

I'm trying to adjust HSV color space same as PhotoShop.

What I've tried :

RGB to HSV, it's seem that is a correct step. But I found HSV adjusting was a lie.Whatever I try to adjust hue\saturation\Bright, the result isn't equal with PS.

Finally, I changed my mind to make a transform to HSL, I got a same result.But the result just is correct under S less than V(RGB to HSV).

With S more than V, there is another situation in PS, The saturation will be change with a changing of light (RGB To HSL), it's Nonlinear relation that I can't make simulation.

Steps:

  1. RGB to HSL;

  2. RGB to HSV;

  3. Code logic...

       if(HSV.S  less than HSV.V);
    
       Adjust HSL; //H range = -180 to180, Sat = -1 to 1, Light = -1 to 1;
    

    The function

       if(S or L less than 0)
    
        _S=S+S*Op;
    
        _L=L+L*Op;
    
       else
    
        _S=S+(1-S)*Op
    
        _L=L+(1-L)*Op
    
        HSL TO RGB
    
    1. If (HSV.S more than HSV.V)

      Nonlinear relation //If add L value,the S will be reduce.
      

HLSL Code:

     //RGB to HSL

    Color.xyz=RGB_TO_HSL(Color.xyz);

     //Adjustment of increment
      //x ~hue  [-180,180]
      //y ~saturation  [-1,1]or[-100%,100%]
      //z ~bright      [-1,1]or[-100%,100%]
    float3 Op = HSVOperate[basehsv].xyz;

    //H

    float Flag = Color.x;

    Color.x += Op.x;

    if (Color.x > 360)

        Color.x -= 360;

    else if(Color.x<0)

        Color.x += 360;

    //S
    float Gap = Max - Min;

    float S, V;

    if (Max == 0)

        S = 0;

    else

        S = Gap / Max;

    V = Max;

    if (S <=V){

        if (Op.z<0)

            Color.z = Color.z + Color.z*Op.z;

        else

            Color.z = Color.z + (1 - Color.z)*Op.z;

        if (Op.y < 0)

            Color.y = Color.y + Color.y*Op.y;

        else

            Color.y = Color.y + (1 - Color.y)*Op.y;

    }

    else if(S >V){

         // process Blue as same as S<V; I don't know why result was

         // correct.

        if (Flag <= 270 && Flag > 210)

        {
            if (Op.z<0)

                Color.z = Color.z + Color.z*Op.z;

            else

                Color.z = Color.z + (1 - Color.z)*Op.z;

            if (Op.y < 0)

                Color.y = Color.y + Color.y*Op.y;

            else

                Color.y = Color.y + (1 - Color.y)*Op.y;

        }

        else


        {   

              // Add directly for Saturation

                Color.z = Color.z +Op.z;

                if (Color.y > 1)

                    Color.y = 1;

                if(Color.y<0)

                    Color.y = 0;
                 //But bright will has a influence  to Saturation
                 // ? 
                 // ? I can't simulate the relation using matlab,
                 // Because   different S and V,different relation.

        }

    }

    //L
    Color.xyz=HSL_TO_RGB(Color.xyz);
VC.One
  • 14,790
  • 4
  • 25
  • 57
F.Eazism
  • 43
  • 10
  • (1) Tag the programming language used (2) Write testable code (where does `_S`, `Op` and S` come from? what are example values?) etc (3) Are you looping through each pixel or using a colorMatrix filter? (4) In Photoshop the **lightness** slider starts with zero at middle so downwards `0 to minus 100` adjusts _Brightness_ (sometimes called _Value_) by adding more black (eg: reduce each R/G/B amount downwards)... Going upwards `0 to plus 100` will adjust _Lightness_ by adding more white (eg: increase each R/G/B amount). – VC.One Sep 15 '17 at 09:49
  • The code was written using HLSL language,I will show it immediately. – F.Eazism Sep 15 '17 at 10:48
  • I don't work with shaders only direct R/G/B hex values. Did you understand my explanation (at point 4) in above comment? What happens when you implement it as HLSL code? To understand it... In PShop just open an image and add 2 layers above it (one layer has white fill and other layer has black fill). You can't see original image now, right? So set the 2 layers each **opacity** slider to zero. To simulate PS Lightness `0 to +100` increase opacity of white layer only (50% opacity is like setting `+50` in Lightness slider) and for Lightness of `0 to -100` hide white and edit the black opacity. – VC.One Sep 15 '17 at 15:15
  • PS: Once you can see that in action, just write HSLS code to achieve same thing. I don't think you need to convert to HSL or HSB/V space since it's a simple thing. Just remember in PShop you see one slider but really the code is in two parts depending on `If` statement checking slider position (eg: If higher than zero do "increase" logic on color value = lighten, `Else if` lower than zero then do "reduce" logic on color value = darken) – VC.One Sep 15 '17 at 15:34
  • I have to transform RGB to HSL,because the result is correct with S – F.Eazism Sep 16 '17 at 03:48
  • I was required to process color-space for movie/vedio making.So I get the RGB ,transform color-space,process changing. maybe you have a issue why make it same as PShop. Because some special filter implemented come from PShop. So I need to apply to my video processing. – F.Eazism Sep 16 '17 at 04:03
  • I asked my boss that why not give a power to process video's color directly. He said that was too difficult to use for user . – F.Eazism Sep 16 '17 at 04:12
  • Photoshop is only adjusting the Hue-Sat-Lightness/Brightness of RGB pixel values. It's not related specifically to color spaces like HSL or HSV/B. If you adjust Lightness while in RGB space it won't affect others like Saturation. That block `if (S <=V) { add something, and multiply something ..etc }` is not helping to my eyes but maybe in shader coding it's needed? I'll explain more later.. – VC.One Sep 16 '17 at 05:47
  • I know a formula adjusting S and L,but I did't read another adjusting Hue without hsl or hav color space. There is another operation just adjusting S and V in Photoshop. I have simulated it,and got correct result. – F.Eazism Sep 16 '17 at 09:01

0 Answers0