Skip to content

Colors

COLOR HELPERS

NO_DIALOG = DialogModes.DisplayNoDialogs module-attribute

CONVERTING COLOR

add_color_to_gradient(action_list, color, location, midpoint)

@param action_list: Action list to add this color to. @param color: SolidColor object @param location: Location of the color along the track. @param midpoint: Percentage midpoint between this color and the next.

Source code in src/helpers/colors.py
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def add_color_to_gradient(
    action_list: ActionList,
    color: SolidColor,
    location: int,
    midpoint: int
) -> None:
    """

    @param action_list: Action list to add this color to.
    @param color: SolidColor object
    @param location: Location of the color along the track.
    @param midpoint: Percentage midpoint between this color and the next.
    """
    action = ActionDescriptor()
    apply_color(action, color)
    action.putEnumerated(sID("type"), sID("colorStopType"), sID("userStop"))
    action.putInteger(sID("location"), location)
    action.putInteger(sID("midpoint"), midpoint)
    action_list.putObject(sID("colorStop"), action)

apply_cmyk(action, c, color_type='color')

Apply CMYK SolidColor object to action descriptor. @param action: ActionDescriptor object. @param c: SolidColor object matching CMYK model. @param color_type: Color action descriptor type, defaults to 'color'.

Source code in src/helpers/colors.py
212
213
214
215
216
217
218
219
def apply_cmyk(action: ActionDescriptor, c: SolidColor, color_type: str = 'color') -> None:
    """
    Apply CMYK SolidColor object to action descriptor.
    @param action: ActionDescriptor object.
    @param c: SolidColor object matching CMYK model.
    @param color_type: Color action descriptor type, defaults to 'color'.
    """
    apply_cmyk_from_list(action, [c.cmyk.cyan, c.cmyk.magenta, c.cmyk.yellow, c.cmyk.black], color_type)

apply_cmyk_from_list(action, color, color_type='color')

Applies CMYK color to action descriptor from a list of values. @param action: ActionDescriptor object. @param color: List of integers for R, G, B. @param color_type: Color action descriptor type, defaults to 'color'.

Source code in src/helpers/colors.py
187
188
189
190
191
192
193
194
195
196
197
198
199
def apply_cmyk_from_list(action: ActionDescriptor, color: list[int], color_type: str = 'color') -> None:
    """
    Applies CMYK color to action descriptor from a list of values.
    @param action: ActionDescriptor object.
    @param color: List of integers for R, G, B.
    @param color_type: Color action descriptor type, defaults to 'color'.
    """
    ad = ActionDescriptor()
    ad.putDouble(sID("cyan"), color[0])
    ad.putDouble(sID("magenta"), color[1])
    ad.putDouble(sID("yellowColor"), color[2])
    ad.putDouble(sID("black"), color[3])
    action.putObject(sID(color_type), sID("CMYKColorClass"), ad)

apply_color(action, color, color_type='color')

Applies color to the specified action descriptor. @param action: ActionDescriptor object. @param color: RGB/CMYK SolidColor object, or list of RGB/CMYK values. @param color_type: Color action descriptor type, defaults to 'color'.

Source code in src/helpers/colors.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def apply_color(action: ActionDescriptor, color: Union[list[int], SolidColor], color_type: str = 'color') -> None:
    """
    Applies color to the specified action descriptor.
    @param action: ActionDescriptor object.
    @param color: RGB/CMYK SolidColor object, or list of RGB/CMYK values.
    @param color_type: Color action descriptor type, defaults to 'color'.
    """
    if isinstance(color, list):
        # List notation
        return apply_rgb_from_list(action, color, color_type) if (
            len(color) < 4
        ) else apply_cmyk_from_list(action, color, color_type)
    if color.model == ColorModel.RGBModel:
        # RGB SolidColor object
        return apply_rgb(action, color, color_type)
    if color.model == ColorModel.CMYKModel:
        # CMYK SolidColor object
        return apply_cmyk(action, color, color_type)
    raise ValueError(f"Received unsupported color object: {color}")

apply_rgb(action, c, color_type='color')

Apply RGB SolidColor object to action descriptor. @param action: ActionDescriptor object. @param c: SolidColor object matching RGB model. @param color_type: Color action descriptor type, defaults to 'color'.

Source code in src/helpers/colors.py
202
203
204
205
206
207
208
209
def apply_rgb(action: ActionDescriptor, c: SolidColor, color_type: str = 'color') -> None:
    """
    Apply RGB SolidColor object to action descriptor.
    @param action: ActionDescriptor object.
    @param c: SolidColor object matching RGB model.
    @param color_type: Color action descriptor type, defaults to 'color'.
    """
    apply_rgb_from_list(action, [c.rgb.red, c.rgb.green, c.rgb.blue], color_type)

apply_rgb_from_list(action, color, color_type='color')

Applies RGB color to action descriptor from a list of values. @param action: ActionDescriptor object. @param color: List of integers for R, G, B. @param color_type: Color action descriptor type, defaults to 'color'.

Source code in src/helpers/colors.py
173
174
175
176
177
178
179
180
181
182
183
184
def apply_rgb_from_list(action: ActionDescriptor, color: list[int], color_type: str = 'color') -> None:
    """
    Applies RGB color to action descriptor from a list of values.
    @param action: ActionDescriptor object.
    @param color: List of integers for R, G, B.
    @param color_type: Color action descriptor type, defaults to 'color'.
    """
    ad = ActionDescriptor()
    ad.putDouble(sID("red"), color[0])
    ad.putDouble(sID("green"), color[1])
    ad.putDouble(sID("blue"), color[2])
    action.putObject(sID(color_type), sID("RGBColor"), ad)

fill_layer_primary()

Fill active layer using foreground color.

Source code in src/helpers/colors.py
264
265
266
267
268
def fill_layer_primary():
    """Fill active layer using foreground color."""
    desc1 = ActionDescriptor()
    desc1.putEnumerated(sID("using"), sID("fillContents"), sID("foregroundColor"))
    app.executeAction(sID("fill"), desc1, NO_DIALOG)

get_cmyk(c, m, y, k)

Creates a SolidColor object with the given CMYK values. @param c: Float from 0.0 to 100.0 for Cyan component. @param m: Float from 0.0 to 100.0 for Magenta component. @param y: Float from 0.0 to 100.0 for Yellow component. @param k: Float from 0.0 to 100.0 for black component. @return: SolidColor object.

Source code in src/helpers/colors.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def get_cmyk(c: float, m: float, y: float, k: float) -> SolidColor:
    """
    Creates a SolidColor object with the given CMYK values.
    @param c: Float from 0.0 to 100.0 for Cyan component.
    @param m: Float from 0.0 to 100.0 for Magenta component.
    @param y: Float from 0.0 to 100.0 for Yellow component.
    @param k: Float from 0.0 to 100.0 for black component.
    @return: SolidColor object.
    """
    color = SolidColor()
    color.cmyk.cyan = c
    color.cmyk.magenta = m
    color.cmyk.yellow = y
    color.cmyk.black = k
    return color

get_color(color)

Automatically get either cmyk or rgb color given a range of @param color: Array containing 3 (RGB) or 4 (CMYK) numbers between 0 and 255, or the name of a known color. @return: SolidColor object.

Source code in src/helpers/colors.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def get_color(color: Union[SolidColor, list[int], str, dict]) -> SolidColor:
    """
    Automatically get either cmyk or rgb color given a range of
    @param color: Array containing 3 (RGB) or 4 (CMYK) numbers between 0 and 255, or the name of a known color.
    @return: SolidColor object.
    """
    try:
        if isinstance(color, SolidColor):
            # Solid color given
            return color
        if isinstance(color, dict):
            # Color dictionary
            if 'r' in color.keys():
                # RGB
                return get_rgb(color['r'], color['g'], color['b'])
            elif 'c' in color.keys():
                # CMYK
                return get_cmyk(color['c'], color['m'], color['y'], color['k'])
        if isinstance(color, str):
            # Named color
            if color in con.colors:
                return get_color(con.colors[color])
            # Hexadecimal
            return get_rgb_from_hex(color)
        if isinstance(color, list):
            # List notation
            if len(color) == 3:
                # RGB
                return get_rgb(*color)
            elif len(color) == 4:
                # CMYK
                return get_cmyk(*color)
    except (ValueError, TypeError):
        raise ValueError(f"Invalid color notation given: {color}")
    raise ValueError(f"Unrecognized color notation given: {color}")

get_pinline_gradient(colors, color_map=None, location_map=None)

Return a gradient color list notation for some given pinline colors. @param colors: Pinline colors to produce a gradient. @param color_map: Color map to color the pinlines. @param location_map: Location map to position gradients. @return: Gradient color list notation.

Source code in src/helpers/colors.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
def get_pinline_gradient(
        colors: str,
        color_map: Optional[dict] = None,
        location_map: dict = None
) -> Union[SolidColor, list[dict]]:
    """
    Return a gradient color list notation for some given pinline colors.
    @param colors: Pinline colors to produce a gradient.
    @param color_map: Color map to color the pinlines.
    @param location_map: Location map to position gradients.
    @return: Gradient color list notation.
    """
    # Establish the color_map
    if not color_map:
        color_map = pinline_color_map

    # Establish the location map
    if not location_map:
        location_map = con.gradient_locations

    # Return our colors
    if not colors:
        return get_color(color_map.get('Artifact', [0, 0, 0]))
    if len(colors) == 1:
        return get_color(color_map.get(colors, [0, 0, 0]))
    if len(colors) == 2:
        return [
            {
                'color': get_color(color_map.get(colors[0], [0, 0, 0])),
                'location': location_map[2][0] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[1], [0, 0, 0])),
                'location': location_map[2][1] * 4096, 'midpoint': 50,
            }
        ]
    if len(colors) == 3:
        return [
            {
                'color': get_color(color_map.get(colors[0], [0, 0, 0])),
                'location': location_map[3][0] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[1], [0, 0, 0])),
                'location': location_map[3][1] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[1], [0, 0, 0])),
                'location': location_map[3][2] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[2], [0, 0, 0])),
                'location': location_map[3][3] * 4096, 'midpoint': 50
            }
        ]
    if len(colors) == 4 and colors not in [LAYERS.LAND, LAYERS.GOLD]:
        return [
            {
                'color': get_color(color_map.get(colors[0], [0, 0, 0])),
                'location': location_map[4][0] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[1], [0, 0, 0])),
                'location': location_map[4][1] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[1], [0, 0, 0])),
                'location': location_map[4][2] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[2], [0, 0, 0])),
                'location': location_map[4][3] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[2], [0, 0, 0])),
                'location': location_map[4][4] * 4096, 'midpoint': 50
            },
            {
                'color': get_color(color_map.get(colors[3], [0, 0, 0])),
                'location': location_map[4][5] * 4096, 'midpoint': 50
            }
        ]
    return get_color(color_map.get(colors, [0, 0, 0]))

get_rgb(r, g, b)

Creates a SolidColor object with the given RGB values. @param r: Integer from 0 to 255 for red spectrum. @param g: Integer from 0 to 255 for green spectrum. @param b: Integer from 0 to 255 for blue spectrum. @return: SolidColor object.

Source code in src/helpers/colors.py
68
69
70
71
72
73
74
75
76
77
78
79
80
def get_rgb(r: int, g: int, b: int) -> SolidColor:
    """
    Creates a SolidColor object with the given RGB values.
    @param r: Integer from 0 to 255 for red spectrum.
    @param g: Integer from 0 to 255 for green spectrum.
    @param b: Integer from 0 to 255 for blue spectrum.
    @return: SolidColor object.
    """
    color = SolidColor()
    color.rgb.red = r
    color.rgb.green = g
    color.rgb.blue = b
    return color

get_rgb_from_hex(hex_code)

Creates an RGB SolidColor object with the given hex value. Allows prepending # or without. @param hex_code: Hexadecimal color code. @return: SolidColor object.

Source code in src/helpers/colors.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def get_rgb_from_hex(hex_code: str) -> SolidColor:
    """
    Creates an RGB SolidColor object with the given hex value. Allows prepending # or without.
    @param hex_code: Hexadecimal color code.
    @return: SolidColor object.
    """
    # Remove hashtag
    hex_code = hex_code.lstrip('#')
    # Hexadecimal abbreviated
    if len(hex_code) == 3:
        hex_code = "".join([n * 2 for n in hex_code])
    # Convert to RGB
    color = SolidColor()
    color.rgb.hexValue = hex_code
    return color

get_text_layer_color(layer)

Occasionally, Photoshop has issues with retrieving the color of a text layer. This helper guards against errors and null values by defaulting to rgb_black() in the event of a problem. @param layer: Layer object that must be TextLayer @return: SolidColor object representing the color of the text item.

Source code in src/helpers/colors.py
154
155
156
157
158
159
160
161
162
163
164
165
def get_text_layer_color(layer: ArtLayer) -> SolidColor:
    """
    Occasionally, Photoshop has issues with retrieving the color of a text layer. This helper guards
    against errors and null values by defaulting to rgb_black() in the event of a problem.
    @param layer: Layer object that must be TextLayer
    @return: SolidColor object representing the color of the text item.
    """
    if isinstance(layer, ArtLayer) and layer.kind == LayerKind.TextLayer:
        if hasattr(layer.textItem, 'color'):
            return layer.textItem.color
        print(f"Couldn't retrieve color of layer: {layer.name}")
    return rgb_black()

hex_to_rgb(color)

Convert a hexadecimal color code into RGB value list. @param color: Hexadecimal color code, e.g. #F5D676 @return: Color in RGB list notation.

Source code in src/helpers/colors.py
28
29
30
31
32
33
34
35
36
def hex_to_rgb(color: str) -> list[int]:
    """
    Convert a hexadecimal color code into RGB value list.
    @param color: Hexadecimal color code, e.g. #F5D676
    @return: Color in RGB list notation.
    """
    if '#' in color:
        color = color[1:]
    return [int(color[i:i+2], 16) for i in (0, 2, 4)]

rgb_black()

Creates a black SolidColor object. @return: SolidColor object.

Source code in src/helpers/colors.py
44
45
46
47
48
49
def rgb_black() -> SolidColor:
    """
    Creates a black SolidColor object.
    @return: SolidColor object.
    """
    return get_rgb(0, 0, 0)

rgb_grey()

Creates a grey SolidColor object. @return: SolidColor object.

Source code in src/helpers/colors.py
52
53
54
55
56
57
def rgb_grey() -> SolidColor:
    """
    Creates a grey SolidColor object.
    @return: SolidColor object.
    """
    return get_rgb(170, 170, 170)

rgb_white()

Creates a white SolidColor object. @return: SolidColor object.

Source code in src/helpers/colors.py
60
61
62
63
64
65
def rgb_white() -> SolidColor:
    """
    Creates a white SolidColor object.
    @return: SolidColor object.
    """
    return get_rgb(255, 255, 255)