Monday, April 11, 2011

Assignment 4.1 (complete)

Part 2

Here's the code for the color blind script I finally finished. I believe this is what you call a Deuteranopia color blindness, affecting someone's ability to distinguish red and green. What I did was create 2 copies of the background layer then use a channel mixer to make a red and green channel from the copies of the original image. I then merged them together and set them as the new red and green channels.

//Color blindness

var curDoc = activeDocument

//create the layer for the red channel
var layerRed = curDoc.artLayers.add()
layerRed.name = "Red Channel"

curDoc.artLayers["Background"].copy()
curDoc.paste()

//channel mixer
var idChnM = charIDToTypeID( "ChnM" );
var desc30 = new ActionDescriptor();
var idpresetKind = stringIDToTypeID( "presetKind" );
var idpresetKindType = stringIDToTypeID( "presetKindType" );
var idpresetKindFactory = stringIDToTypeID( "presetKindFactory" );
desc30.putEnumerated( idpresetKind, idpresetKindType, idpresetKindFactory );
var idUsng = charIDToTypeID( "Usng" );
desc30.putPath( idUsng, new File( "/Applications/Adobe Photoshop CS5/Presets/Channel Mixer/Black & White with Red Filter (RGB).cha" ) );
executeAction( idChnM, desc30, DialogModes.NO );


//create the layer for the green channel

var layerGreen = curDoc.artLayers.add()
layerGreen.name = "Green Channel"

curDoc.artLayers["Background"].copy()
curDoc.paste()

//channel mixer
var idChnM = charIDToTypeID( "ChnM" );
var desc4049 = new ActionDescriptor();
var idpresetKind = stringIDToTypeID( "presetKind" );
var idpresetKindType = stringIDToTypeID( "presetKindType" );
var idpresetKindFactory = stringIDToTypeID( "presetKindFactory" );
desc4049.putEnumerated( idpresetKind, idpresetKindType, idpresetKindFactory );
var idUsng = charIDToTypeID( "Usng" );
desc4049.putPath( idUsng, new File( "/Applications/Adobe Photoshop CS5/Presets/Channel Mixer/Black & White with Green Filter (RGB).cha" ) );
executeAction( idChnM, desc4049, DialogModes.NO );

var globalOpacity = 50.0;
layerGreen.opacity = globalOpacity;

//Merge layers
var idMrgtwo = charIDToTypeID( "Mrg2" );
var desc243 = new ActionDescriptor();
executeAction( idMrgtwo, desc243, DialogModes.NO );

curDoc.selection.selectAll()
curDoc.selection.copy()
curDoc.activeLayer.remove()

//paste the combined red/green channel into the red channel.
redChannel = new Array(curDoc.channels[0])
curDoc.activeChannels = redChannel

activeDocument.paste();

//paste the combined red/green channel into the green channel.
greenChannel = new Array(curDoc.channels[1])
curDoc.activeChannels = greenChannel

activeDocument.paste();

//make all three channels visible again
activeDocument.activeChannels = activeDocument.componentChannels

//save file
curDoc.flatten();
saveFile = new File("//color_blind_image.jpg");
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 9;
curDoc.saveAs(saveFile, saveOptions, true,Extension.LOWERCASE);


I had to use code from the script listener a couple times because I couldn't find or figure out how to do it otherwise. I used it for the channel mixer command and for merging layers.