Decrease saturation of highly saturated colors?

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

Mike Hale

Decrease saturation of highly saturated colors?

Post by Mike Hale »

I guess that it is possible that I still misunderstand what is needed. I thought that just as saturation levels above 90 needed to be clipped, the brightness values below 5 also need to be clipped. So RGB 255,0,0 would be ok while RGB 3,3,3 would not.

However if RGB 255,0,0 needs to be changed to 255,5,5 then my second script does not do what is needed. In that case I would add david's code to my first script. I don't think the script needs to clip both the brightness channel and each RGB channel.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

david

Decrease saturation of highly saturated colors?

Post by david »

Of course, if you lower the saturation to begin with, you'll won't have numbers like 255,0,0 anyway.
BigSwopa

Decrease saturation of highly saturated colors?

Post by BigSwopa »

Thanks to both of you!

@ Mike When I apply your second script to a pixel with RGB 6 6 6 it makes RGB 11 11 11 of it. However, it should leave the value unchanged, because it is greater than R G B 5 5 5.
@ david Your script changed a pure white to a very bright red. Am I doing something wrong?

I guess I was confusing you and myself by mixing HSB and RGB values. Or maybe I was posting prematurely before really knowing what I want or being able to properly explain it. I'm sorry

In terms of 3D rendering it is probably easier to talk about HSB only. As I said in my first post no pixel may have an S value above 90. Additionally all pixels need to have a B value of at least 2 and at most 90, i.e. a pixel with a B value of 3 shall be unaffected by the script (of course only if its S value is within the valid range as well). A pixel with B = 0 or B = 1 needs to be raised to B = 2.

I really do not know if the image needs to be processed per channel or per pixel. I only do a minimal amount of work in Photoshop

You guys are amazingly fast at scripting! Enviable, really...
david

Decrease saturation of highly saturated colors?

Post by david »

Make sure mine is being run while in RGB mode
BigSwopa

Decrease saturation of highly saturated colors?

Post by BigSwopa »

Ok, I don't get that "very bright red" problem anymore, but your script also changes an RGB 5 5 5 value to RGB 11 11 11. Is that easy to fix?
Mike Hale

Decrease saturation of highly saturated colors?

Post by Mike Hale »

BigSwopa wrote:I guess I was confusing you and myself by mixing HSB and RGB values.

That is ok, we are all here to learn. Without david's suggestion about the hsb filter I would not have known this could be done. It doesn't help that the output of that filter is still really a RGB document so the values are 0-255 instead of 0-100 as they would be if Photoshop had a true hsb mode.

The script below uses david's suggestion of filling with blendmodes to clip the levels. It does that in the brightness and saturation channels while the document is in the hsb state. I still may be wrong, but from what you have posted, I don't think you want to adjust the brightness in RGB.

It also uses suspendHistory so it only takes up one history state. That not only makes it easier to undo but saves using more of the limited historystates than needed.

Code: Select allapp.activeDocument.suspendHistory('Limit sat and lum levels', 'prepImage()');
function prepImage(){
      convertRGBToHSB();
       makeChannelActiveByIndex( 3, true );// select the blue/brightness channel
       setMinimumChannelValue(6);// rgb 6,6,6 = hsb x,x,2
      setMaximumchannelValue(230);//rgb 230 = hsb x,x,90
        makeChannelActiveByIndex( 2, true );// select the green/saturation channel
        setMaximumchannelValue(230);
        selectComponetChannel();// slect the componet channel
        convertHSBToRGB();

        function convertRGBToHSB(){
            var desc = new ActionDescriptor();
            desc.putEnumerated( charIDToTypeID( "Inpt" ), charIDToTypeID( "ClrS" ), charIDToTypeID( "RGBC" ) );
            desc.putEnumerated( charIDToTypeID( "Otpt" ), charIDToTypeID( "ClrS" ), charIDToTypeID( "HSBl" ) );
           executeAction( charIDToTypeID( "HsbP" ), desc, DialogModes.NO );
        };

      function setMinimumChannelValue(min){
         var desc = new ActionDescriptor();
         desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "FlCn" ), charIDToTypeID( "Clr " ) );
         var colorDesc = new ActionDescriptor();
         colorDesc.putDouble( charIDToTypeID( "Rd  " ), min-0.1 );
         colorDesc.putDouble( charIDToTypeID( "Grn " ), min-0.1 );
         colorDesc.putDouble( charIDToTypeID( "Bl  " ), min-0.1 );
         desc.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), colorDesc );
         desc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), 100.000000 );
         desc.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Lghn" ) );
         executeAction( charIDToTypeID( "Fl  " ), desc, DialogModes.NO );
      };
      function setMaximumchannelValue(max){
         var desc = new ActionDescriptor();
         desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "FlCn" ), charIDToTypeID( "Clr " ) );
         var colorDesc = new ActionDescriptor();
         colorDesc.putDouble( charIDToTypeID( "Rd  " ), max-0.1 );
         colorDesc.putDouble( charIDToTypeID( "Grn " ), max-0.1 );
         colorDesc.putDouble( charIDToTypeID( "Bl  " ), max-0.1 );
         desc.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), colorDesc );
         desc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), 100.000000 );
         desc.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Drkn" ) );
         executeAction( charIDToTypeID( "Fl  " ), desc, DialogModes.NO );
      };
        function convertHSBToRGB(){
            var desc = new ActionDescriptor();
            desc.putEnumerated( charIDToTypeID( "Inpt" ), charIDToTypeID( "ClrS" ), charIDToTypeID( "HSBl" ) );
            desc.putEnumerated( charIDToTypeID( "Otpt" ), charIDToTypeID( "ClrS" ), charIDToTypeID( "RGBC" ) );
           executeAction( charIDToTypeID( "HsbP" ), desc, DialogModes.NO );
        };
        function makeChannelActiveByIndex( idx, visible ){
            var desc = new ActionDescriptor();
              var ref = new ActionReference();
              ref.putIndex(charIDToTypeID( "Chnl" ), idx)
              desc.putReference( charIDToTypeID( "null" ), ref );
              desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
           executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
        };
        function selectComponetChannel() {
            try{
                var map = {}
                map[DocumentMode.GRAYSCALE] = charIDToTypeID('Blck');
                map[DocumentMode.RGB] = charIDToTypeID('RGB ');
                map[DocumentMode.CMYK] = charIDToTypeID('CMYK');
                map[DocumentMode.LAB] = charIDToTypeID('Lab ');
                var desc = new ActionDescriptor();
                    var ref = new ActionReference();
                    ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), map[app.activeDocument.mode] );
                desc.putReference( charIDToTypeID('null'), ref );
                executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
            }catch(e){}
        };
};