Last active
December 4, 2022 04:02
-
-
Save tokyosheep/8f71fa49d3aa466d366cd3d05687fb7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| this script creates solid color fill layer from gradient. | |
| use this script as a psjs. | |
| before executing script, select a gradient layer. | |
| it creates solid color fill layer following point of color on gradeint layer. | |
| requirement | |
| later than Photoshop ver 23.5 | |
| now only available on CMYK and RGB mode. | |
| */ | |
| const batchPlay = require("photoshop").action.batchPlay; | |
| const { app } = require("photoshop"); | |
| const script = await require("uxp").script; | |
| const executionContext = script.executionContext; | |
| const { hostControl } = executionContext; | |
| const RGBMODE = 'RGBColorMode'; | |
| const CMYKMODE = 'CMYKColorMode'; | |
| const getJSONLayers = async () => { | |
| /* | |
| it can get adjustment data as a json data. | |
| */ | |
| const result = await batchPlay( | |
| [ | |
| { | |
| _obj: 'get', | |
| _target: [ | |
| { | |
| _property: 'json' | |
| }, | |
| { | |
| _ref: 'document', | |
| _enum: 'ordinal' | |
| } | |
| ], | |
| layerInfo: true, | |
| includeAncestors: true, | |
| _options: { | |
| dialogOptions: 'dontDisplay' | |
| } | |
| } | |
| ], {} | |
| ); | |
| const adjustData = JSON.parse(result[0].json); | |
| console.log(adjustData); | |
| return adjustData.layers; | |
| }; | |
| /** | |
| * get the active layer id | |
| * @returns @type {string} | |
| */ | |
| const getActiveLayer = async () => { | |
| if( app.activeDocument.activeLayers.length > 1) { | |
| await app.showAlert('please select just a layer'); | |
| return null; | |
| } | |
| const activeLayer = app.activeDocument.activeLayers[0]; | |
| console.log(activeLayer.kind); | |
| if(activeLayer.kind !== 'gradientFill') { | |
| await app.showAlert('select gradient layer'); | |
| return null; | |
| } | |
| return activeLayer.id; | |
| }; | |
| /** | |
| * find target layer from json data which comea from batchPlay. | |
| * @param jsonLayers @type {Object} | |
| * @param id @type {string} | |
| * @returns @type {Layer Object} | |
| */ | |
| const findTargetLayer = (jsonLayers, id) => { | |
| for (let i=0;i<jsonLayers.length;i++) { | |
| console.log(jsonLayers[i].id, id); | |
| if(jsonLayers[i].id === id) { | |
| return jsonLayers[i]; | |
| } | |
| if(jsonLayers[i].type === 'layerSection' && jsonLayers[i].layers) { | |
| const result = findTargetLayer(jsonLayers[i].layers, id); | |
| if (result !== null) return result; | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * finding target gradient layer | |
| * @returns @type {JSONLayer} | |
| */ | |
| const selectGradient = async () => { | |
| const id = await getActiveLayer(); | |
| if(id === null)return; | |
| const jsonLayers = await getJSONLayers(); | |
| const jsonLayer = findTargetLayer(jsonLayers, id); | |
| return jsonLayer; | |
| } | |
| /** | |
| * Gradient class | |
| * this one generates solid color layer | |
| */ | |
| class Gradient { | |
| constructor (jsonLayer) { | |
| this.mode = app.activeDocument.mode; | |
| this.colors = [...jsonLayer.fill.gradient.colors]; | |
| this.transparency = [...jsonLayer.fill.gradient.transparency]; | |
| }; | |
| /** | |
| * create cmyk color | |
| * and recieved color obj | |
| * @param colorObj @type {colorObj | |
| * cyan: number | |
| * magenta: number | |
| * yellowColor: number | |
| * black: number | |
| * } | |
| */ | |
| async createCMYKFillColor (colorObj) { | |
| const result = await batchPlay( | |
| [ | |
| { | |
| _obj: "make", | |
| _target: [ | |
| { | |
| _ref: "contentLayer" | |
| } | |
| ], | |
| using: { | |
| _obj: "contentLayer", | |
| type: { | |
| _obj: "solidColorLayer", | |
| color: { | |
| _obj: "CMYKColorClass", | |
| ...colorObj | |
| } | |
| } | |
| }, | |
| _options: { | |
| dialogOptions: "dontDisplay" | |
| } | |
| } | |
| ],{ | |
| }); | |
| return result; | |
| } | |
| /** | |
| * @param colorObj @type { | |
| * color: { | |
| * red: number | |
| * grain: number | |
| * blue: number | |
| * }} | |
| */ | |
| async createRGBFillColor (colorObj) { | |
| const result = await batchPlay( | |
| [ | |
| { | |
| _obj: "make", | |
| _target: [ | |
| { | |
| _ref: "contentLayer" | |
| } | |
| ], | |
| using: { | |
| _obj: "contentLayer", | |
| type: { | |
| _obj: "solidColorLayer", | |
| color: { | |
| _obj: "RGBColor", | |
| ...colorObj | |
| } | |
| } | |
| }, | |
| _options: { | |
| dialogOptions: "dontDisplay" | |
| } | |
| } | |
| ],{ | |
| }); | |
| return result; | |
| }; | |
| async createFillFromNumber (num) { | |
| try { | |
| if (this.mode === 'RGBColorMode') { | |
| this.createRGBFillColor(this.colors[num].color); | |
| } else { | |
| this.createCMYKFillColor(this.colors[num].color); | |
| } | |
| return null; | |
| } catch (e) { | |
| return e.message; | |
| } | |
| } | |
| /** | |
| * create solid color layer from you got gradient data. | |
| */ | |
| async createAllOfColors () { | |
| const msgs = []; | |
| console.log(this.colors.length); | |
| for (let i = 0;i < this.colors.length; i++) { | |
| const r = await this.createFillFromNumber(i); | |
| msgs.push(r); | |
| } | |
| const errorMSG = msgs.filter(msg => msg !== null); | |
| } | |
| }; | |
| try { | |
| if (app.activeDocument.mode !== RGBMODE && app.activeDocument.mode !== CMYKMODE ) { | |
| await app.showAlert("it's invalid color mode"); | |
| return; | |
| } | |
| const suspensionID = await hostControl.suspendHistory({ | |
| documentID: app.activeDocument.id, | |
| name: 'separate gradient' | |
| }); | |
| const jsonLayer = await selectGradient(); | |
| if (jsonLayer !== null) { | |
| const gradient = new Gradient(jsonLayer); | |
| await gradient.createAllOfColors(); | |
| } | |
| await hostControl.resumeHistory(suspensionID); | |
| } catch (e) { | |
| app.showAlert(e); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment