Skip to content

Colors

src.helpers.colors.hex_to_rgb(color: str) -> list[int]

Convert a hexadecimal color code into RGB value list.

Parameters:

Name Type Description Default
color str

Hexadecimal color code, e.g. #F5D676

required
Source code in src\helpers\colors.py
26
27
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.

    Args:
        color: Hexadecimal color code, e.g. #F5D676
    Returns:
        Color in RGB list notation.
    """
    if '#' in color:
        color = color[1:]
    return [int(color[i:i+2], 16) for i in (0, 2, 4)]

src.helpers.colors.rgb_black() -> SolidColor

Creates a black SolidColor object.

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
44
45
46
47
48
49
50
def rgb_black() -> SolidColor:
    """Creates a black SolidColor object.

    Returns:
        SolidColor object.
    """
    return get_rgb(0, 0, 0)

src.helpers.colors.rgb_grey() -> SolidColor

Creates a grey SolidColor object.

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
53
54
55
56
57
58
59
def rgb_grey() -> SolidColor:
    """Creates a grey SolidColor object.

    Returns:
        SolidColor object.
    """
    return get_rgb(170, 170, 170)

src.helpers.colors.rgb_white() -> SolidColor

Creates a white SolidColor object.

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
62
63
64
65
66
67
68
def rgb_white() -> SolidColor:
    """Creates a white SolidColor object.

    Returns:
        SolidColor object.
    """
    return get_rgb(255, 255, 255)

src.helpers.colors.get_rgb(r: int, g: int, b: int) -> SolidColor

Creates a SolidColor object with the given RGB values.

Parameters:

Name Type Description Default
r int

Integer from 0 to 255 for red spectrum.

required
g int

Integer from 0 to 255 for green spectrum.

required
b int

Integer from 0 to 255 for blue spectrum.

required

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def get_rgb(r: int, g: int, b: int) -> SolidColor:
    """Creates a SolidColor object with the given RGB values.

    Args:
        r: Integer from 0 to 255 for red spectrum.
        g: Integer from 0 to 255 for green spectrum.
        b: Integer from 0 to 255 for blue spectrum.

    Returns:
        SolidColor object.
    """
    color = SolidColor()
    color.rgb.red = r
    color.rgb.green = g
    color.rgb.blue = b
    return color

src.helpers.colors.get_rgb_from_hex(hex_code: str) -> SolidColor

Creates an RGB SolidColor object with the given hex value. Allows prepending # or without.

Parameters:

Name Type Description Default
hex_code str

Hexadecimal color code.

required

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def get_rgb_from_hex(hex_code: str) -> SolidColor:
    """Creates an RGB SolidColor object with the given hex value. Allows prepending # or without.

    Args:
        hex_code: Hexadecimal color code.

    Returns:
        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

src.helpers.colors.get_cmyk(c: float, m: float, y: float, k: float) -> SolidColor

Creates a SolidColor object with the given CMYK values.

Parameters:

Name Type Description Default
c float

Float from 0.0 to 100.0 for Cyan component.

required
m float

Float from 0.0 to 100.0 for Magenta component.

required
y float

Float from 0.0 to 100.0 for Yellow component.

required
k float

Float from 0.0 to 100.0 for black component.

required

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def get_cmyk(c: float, m: float, y: float, k: float) -> SolidColor:
    """Creates a SolidColor object with the given CMYK values.

    Args:
        c: Float from 0.0 to 100.0 for Cyan component.
        m: Float from 0.0 to 100.0 for Magenta component.
        y: Float from 0.0 to 100.0 for Yellow component.
        k: Float from 0.0 to 100.0 for black component.

    Returns:
        SolidColor object.
    """
    color = SolidColor()
    color.cmyk.cyan = c
    color.cmyk.magenta = m
    color.cmyk.yellow = y
    color.cmyk.black = k
    return color

src.helpers.colors.get_color(color: Union[SolidColor, list[int], str]) -> SolidColor

Automatically get either cmyk or rgb color given a range of possible input notations.

Parameters:

Name Type Description Default
color SolidColor | list[int] | str

List of 3 (RGB) or 4 (CMYK) numbers between 0 and 255, the hex of a color, the name of a known color, or a SolidColor object.

required

Returns:

Type Description
SolidColor

SolidColor object.

Source code in src\helpers\colors.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def get_color(color: Union[SolidColor, list[int], str]) -> SolidColor:
    """Automatically get either cmyk or rgb color given a range of possible input notations.

    Args:
        color: List of 3 (RGB) or 4 (CMYK) numbers between 0 and 255, the hex of a color, the name of
            a known color, or a SolidColor object.

    Returns:
        SolidColor object.
    """
    try:
        if isinstance(color, SolidColor):
            # Solid color given
            return 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)
        if isinstance(color, str):
            # Named color
            if color in CON.colors:
                return get_color(CON.colors[color])
            # Hexadecimal
            return get_rgb_from_hex(color)
    except (ValueError, TypeError):
        raise ValueError(f"Invalid color notation given: {color}")
    raise ValueError(f"Unrecognized color notation given: {color}")

src.helpers.colors.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 black in the event of a problem.

Parameters:

Name Type Description Default
layer ArtLayer

Layer object that must be TextLayer

required

Returns:

Type Description
SolidColor

SolidColor object representing the color of the text item.

Source code in src\helpers\colors.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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 black in the event of a problem.

    Args:
        layer: Layer object that must be TextLayer

    Returns:
        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()

src.helpers.colors.get_text_item_color(item: TextItem) -> SolidColor

Utility definition for get_text_layer_color, targeting a known TextItem.

Source code in src\helpers\colors.py
179
180
181
182
183
184
185
def get_text_item_color(item: TextItem) -> SolidColor:
    """Utility definition for `get_text_layer_color`, targeting a known TextItem."""
    if isinstance(item, TextItem):
        if hasattr(item, 'color'):
            return item.color
        print(f"Couldn't retrieve color of TextItem!")
    return rgb_black()

src.helpers.colors.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.

Parameters:

Name Type Description Default
action ActionDescriptor

ActionDescriptor object.

required
color list[int]

List of integers for R, G, B.

required
color_type str

Color action descriptor type, defaults to 'color'.

'color'
Source code in src\helpers\colors.py
193
194
195
196
197
198
199
200
201
202
203
204
205
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.

    Args:
        action: ActionDescriptor object.
        color: List of integers for R, G, B.
        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)

src.helpers.colors.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.

Parameters:

Name Type Description Default
action ActionDescriptor

ActionDescriptor object.

required
color list[int]

List of integers for R, G, B.

required
color_type str

Color action descriptor type, defaults to 'color'.

'color'
Source code in src\helpers\colors.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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.

    Args:
        action: ActionDescriptor object.
        color: List of integers for R, G, B.
        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)

src.helpers.colors.apply_rgb(action: ActionDescriptor, c: SolidColor, color_type: str = 'color') -> None

Apply RGB SolidColor object to action descriptor.

Parameters:

Name Type Description Default
action ActionDescriptor

ActionDescriptor object.

required
c SolidColor

SolidColor object matching RGB model.

required
color_type str

Color action descriptor type, defaults to 'color'.

'color'
Source code in src\helpers\colors.py
224
225
226
227
228
229
230
231
232
def apply_rgb(action: ActionDescriptor, c: SolidColor, color_type: str = 'color') -> None:
    """Apply RGB SolidColor object to action descriptor.

    Args:
        action: ActionDescriptor object.
        c: SolidColor object matching RGB model.
        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)

src.helpers.colors.apply_cmyk(action: ActionDescriptor, c: SolidColor, color_type: str = 'color') -> None

Apply CMYK SolidColor object to action descriptor.

Parameters:

Name Type Description Default
action ActionDescriptor

ActionDescriptor object.

required
c SolidColor

SolidColor object matching CMYK model.

required
color_type str

Color action descriptor type, defaults to 'color'.

'color'
Source code in src\helpers\colors.py
235
236
237
238
239
240
241
242
243
def apply_cmyk(action: ActionDescriptor, c: SolidColor, color_type: str = 'color') -> None:
    """Apply CMYK SolidColor object to action descriptor.

    Args:
        action: ActionDescriptor object.
        c: SolidColor object matching CMYK model.
        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)

src.helpers.colors.apply_color(action: ActionDescriptor, color: Union[list[int], SolidColor, str], color_type: str = 'color') -> None

Applies color to the specified action descriptor.

Parameters:

Name Type Description Default
action ActionDescriptor

ActionDescriptor object.

required
color list[int] | SolidColor | str

RGB/CMYK SolidColor object, list of RGB/CMYK values, a hex string, or a named color.

required
color_type str

Color action descriptor type, defaults to 'color'.

'color'
Source code in src\helpers\colors.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def apply_color(
    action: ActionDescriptor,
    color: Union[list[int], SolidColor, str],
    color_type: str = 'color'
) -> None:
    """Applies color to the specified action descriptor.

    Args:
        action: ActionDescriptor object.
        color: RGB/CMYK SolidColor object, list of RGB/CMYK values, a hex string, or a named color.
        color_type: Color action descriptor type, defaults to 'color'.
    """
    if isinstance(color, list):
        # RGB / CMYK list notation
        if len(color) < 4:
            return apply_rgb_from_list(action, color, color_type)
        return apply_cmyk_from_list(action, color, color_type)
    if isinstance(color, SolidColor):
        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)
    if isinstance(color, str):
        # Named or hex color
        return apply_color(action, get_color(color), color_type)
    raise ValueError(f"Received unsupported color object: {color}")

src.helpers.colors.add_color_to_gradient(action_list: ActionList, color: SolidColor, location: int, midpoint: int) -> None

Adds a SolidColor to gradient at a given location, with a given midpoint.

Parameters:

Name Type Description Default
action_list ActionList

Action list to add this color to.

required
color SolidColor

SolidColor object.

required
location int

Location of the color along the track.

required
midpoint int

Percentage midpoint between this color and the next.

required
Source code in src\helpers\colors.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def add_color_to_gradient(
    action_list: ActionList,
    color: SolidColor,
    location: int,
    midpoint: int
) -> None:
    """Adds a SolidColor to gradient at a given location, with a given midpoint.

    Args:
        action_list: Action list to add this color to.
        color: SolidColor object.
        location: Location of the color along the track.
        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)

src.helpers.colors.fill_layer_primary()

Fill active layer using foreground color.

Source code in src\helpers\colors.py
298
299
300
301
302
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)

src.helpers.colors.get_pinline_gradient(colors: str, color_map: Optional[dict] = None, location_map: dict = None) -> Union[list[int], list[dict]]

Return a gradient color list notation for some given pinline colors.

Parameters:

Name Type Description Default
colors str

Pinline colors to produce a gradient.

required
color_map dict | None

Color map to color the pinlines.

None
location_map dict

Location map to position gradients.

None

Returns:

Type Description
list[int] | list[dict]

Gradient color list notation.

Source code in src\helpers\colors.py
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def get_pinline_gradient(
        colors: str,
        color_map: Optional[dict] = None,
        location_map: dict = None
) -> Union[list[int], list[dict]]:
    """Return a gradient color list notation for some given pinline colors.

    Args:
        colors: Pinline colors to produce a gradient.
        color_map: Color map to color the pinlines.
        location_map: Location map to position gradients.

    Returns:
        Gradient color list notation.
    """
    # Establish the color_map
    if not color_map:
        color_map = pinlines_color_map

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

    # Return our colors
    if not colors:
        return color_map.get('Artifact', [0, 0, 0])
    if len(colors) == 1:
        return color_map.get(colors, [0, 0, 0])
    if len(colors) == 2:
        return [
            {
                'color': color_map.get(colors[0], [0, 0, 0]),
                'location': location_map[2][0] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[1], [0, 0, 0]),
                'location': location_map[2][1] * 4096, 'midpoint': 50,
            }
        ]
    if len(colors) == 3:
        return [
            {
                'color': color_map.get(colors[0], [0, 0, 0]),
                'location': location_map[3][0] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[1], [0, 0, 0]),
                'location': location_map[3][1] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[1], [0, 0, 0]),
                'location': location_map[3][2] * 4096, 'midpoint': 50
            },
            {
                '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': color_map.get(colors[0], [0, 0, 0]),
                'location': location_map[4][0] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[1], [0, 0, 0]),
                'location': location_map[4][1] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[1], [0, 0, 0]),
                'location': location_map[4][2] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[2], [0, 0, 0]),
                'location': location_map[4][3] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[2], [0, 0, 0]),
                'location': location_map[4][4] * 4096, 'midpoint': 50
            },
            {
                'color': color_map.get(colors[3], [0, 0, 0]),
                'location': location_map[4][5] * 4096, 'midpoint': 50
            }
        ]
    return color_map.get(colors, [0, 0, 0])