Source code

Download source archive ↓

src/openpyxl/drawing/colors.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors.serialisable import Serialisable
4from openpyxl.descriptors import (
5    Alias,
6    Typed,
7    Integer,
8    Set,
9    MinMax,
10)
11from openpyxl.descriptors.excel import Percentage
12from openpyxl.descriptors.nested import (
13    NestedNoneSet,
14    NestedValue,
15    NestedInteger,
16    EmptyTag,
17)
18
19from openpyxl.styles.colors import RGB
20from openpyxl.xml.constants import DRAWING_NS
21
22from openpyxl.descriptors.excel import ExtensionList as OfficeArtExtensionList
23
24PRESET_COLORS = [
25        'aliceBlue', 'antiqueWhite', 'aqua', 'aquamarine',
26        'azure', 'beige', 'bisque', 'black', 'blanchedAlmond', 'blue',
27        'blueViolet', 'brown', 'burlyWood', 'cadetBlue', 'chartreuse',
28        'chocolate', 'coral', 'cornflowerBlue', 'cornsilk', 'crimson', 'cyan',
29        'darkBlue', 'darkCyan', 'darkGoldenrod', 'darkGray', 'darkGrey',
30        'darkGreen', 'darkKhaki', 'darkMagenta', 'darkOliveGreen', 'darkOrange',
31        'darkOrchid', 'darkRed', 'darkSalmon', 'darkSeaGreen', 'darkSlateBlue',
32        'darkSlateGray', 'darkSlateGrey', 'darkTurquoise', 'darkViolet',
33        'dkBlue', 'dkCyan', 'dkGoldenrod', 'dkGray', 'dkGrey', 'dkGreen',
34        'dkKhaki', 'dkMagenta', 'dkOliveGreen', 'dkOrange', 'dkOrchid', 'dkRed',
35        'dkSalmon', 'dkSeaGreen', 'dkSlateBlue', 'dkSlateGray', 'dkSlateGrey',
36        'dkTurquoise', 'dkViolet', 'deepPink', 'deepSkyBlue', 'dimGray',
37        'dimGrey', 'dodgerBlue', 'firebrick', 'floralWhite', 'forestGreen',
38        'fuchsia', 'gainsboro', 'ghostWhite', 'gold', 'goldenrod', 'gray',
39        'grey', 'green', 'greenYellow', 'honeydew', 'hotPink', 'indianRed',
40        'indigo', 'ivory', 'khaki', 'lavender', 'lavenderBlush', 'lawnGreen',
41        'lemonChiffon', 'lightBlue', 'lightCoral', 'lightCyan',
42        'lightGoldenrodYellow', 'lightGray', 'lightGrey', 'lightGreen',
43        'lightPink', 'lightSalmon', 'lightSeaGreen', 'lightSkyBlue',
44        'lightSlateGray', 'lightSlateGrey', 'lightSteelBlue', 'lightYellow',
45        'ltBlue', 'ltCoral', 'ltCyan', 'ltGoldenrodYellow', 'ltGray', 'ltGrey',
46        'ltGreen', 'ltPink', 'ltSalmon', 'ltSeaGreen', 'ltSkyBlue',
47        'ltSlateGray', 'ltSlateGrey', 'ltSteelBlue', 'ltYellow', 'lime',
48        'limeGreen', 'linen', 'magenta', 'maroon', 'medAquamarine', 'medBlue',
49        'medOrchid', 'medPurple', 'medSeaGreen', 'medSlateBlue',
50        'medSpringGreen', 'medTurquoise', 'medVioletRed', 'mediumAquamarine',
51        'mediumBlue', 'mediumOrchid', 'mediumPurple', 'mediumSeaGreen',
52        'mediumSlateBlue', 'mediumSpringGreen', 'mediumTurquoise',
53        'mediumVioletRed', 'midnightBlue', 'mintCream', 'mistyRose', 'moccasin',
54        'navajoWhite', 'navy', 'oldLace', 'olive', 'oliveDrab', 'orange',
55        'orangeRed', 'orchid', 'paleGoldenrod', 'paleGreen', 'paleTurquoise',
56        'paleVioletRed', 'papayaWhip', 'peachPuff', 'peru', 'pink', 'plum',
57        'powderBlue', 'purple', 'red', 'rosyBrown', 'royalBlue', 'saddleBrown',
58        'salmon', 'sandyBrown', 'seaGreen', 'seaShell', 'sienna', 'silver',
59        'skyBlue', 'slateBlue', 'slateGray', 'slateGrey', 'snow', 'springGreen',
60        'steelBlue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet',
61        'wheat', 'white', 'whiteSmoke', 'yellow', 'yellowGreen'
62    ]
63
64
65SCHEME_COLORS= ['bg1', 'tx1', 'bg2', 'tx2', 'accent1', 'accent2', 'accent3',
66                'accent4', 'accent5', 'accent6', 'hlink', 'folHlink', 'phClr', 'dk1', 'lt1',
67                'dk2', 'lt2'
68                ]
69
70
71class Transform(Serialisable):
72
73    pass
74
75
76class SystemColor(Serialisable):
77
78    tagname = "sysClr"
79    namespace = DRAWING_NS
80
81    # color transform options
82    tint = NestedInteger(allow_none=True)
83    shade = NestedInteger(allow_none=True)
84    comp = Typed(expected_type=Transform, allow_none=True)
85    inv = Typed(expected_type=Transform, allow_none=True)
86    gray = Typed(expected_type=Transform, allow_none=True)
87    alpha = NestedInteger(allow_none=True)
88    alphaOff = NestedInteger(allow_none=True)
89    alphaMod = NestedInteger(allow_none=True)
90    hue = NestedInteger(allow_none=True)
91    hueOff = NestedInteger(allow_none=True)
92    hueMod = NestedInteger(allow_none=True)
93    sat = NestedInteger(allow_none=True)
94    satOff = NestedInteger(allow_none=True)
95    satMod = NestedInteger(allow_none=True)
96    lum = NestedInteger(allow_none=True)
97    lumOff = NestedInteger(allow_none=True)
98    lumMod = NestedInteger(allow_none=True)
99    red = NestedInteger(allow_none=True)
100    redOff = NestedInteger(allow_none=True)
101    redMod = NestedInteger(allow_none=True)
102    green = NestedInteger(allow_none=True)
103    greenOff = NestedInteger(allow_none=True)
104    greenMod = NestedInteger(allow_none=True)
105    blue = NestedInteger(allow_none=True)
106    blueOff = NestedInteger(allow_none=True)
107    blueMod = NestedInteger(allow_none=True)
108    gamma = Typed(expected_type=Transform, allow_none=True)
109    invGamma = Typed(expected_type=Transform, allow_none=True)
110
111    val = Set(values=( ['scrollBar', 'background', 'activeCaption',
112                        'inactiveCaption', 'menu', 'window', 'windowFrame', 'menuText',
113                        'windowText', 'captionText', 'activeBorder', 'inactiveBorder',
114                        'appWorkspace', 'highlight', 'highlightText', 'btnFace', 'btnShadow',
115                        'grayText', 'btnText', 'inactiveCaptionText', 'btnHighlight',
116                        '3dDkShadow', '3dLight', 'infoText', 'infoBk', 'hotLight',
117                        'gradientActiveCaption', 'gradientInactiveCaption', 'menuHighlight',
118                        'menuBar'] )
119              )
120    lastClr = RGB(allow_none=True)
121
122    __elements__ = ('tint', 'shade', 'comp', 'inv', 'gray', "alpha",
123                    "alphaOff", "alphaMod", "hue", "hueOff", "hueMod", "hueOff", "sat",
124                    "satOff", "satMod", "lum", "lumOff", "lumMod", "red", "redOff", "redMod",
125                    "green", "greenOff", "greenMod", "blue", "blueOff", "blueMod", "gamma",
126                    "invGamma")
127
128    def __init__(self,
129                 val="windowText",
130                 lastClr=None,
131                 tint=None,
132                 shade=None,
133                 comp=None,
134                 inv=None,
135                 gray=None,
136                 alpha=None,
137                 alphaOff=None,
138                 alphaMod=None,
139                 hue=None,
140                 hueOff=None,
141                 hueMod=None,
142                 sat=None,
143                 satOff=None,
144                 satMod=None,
145                 lum=None,
146                 lumOff=None,
147                 lumMod=None,
148                 red=None,
149                 redOff=None,
150                 redMod=None,
151                 green=None,
152                 greenOff=None,
153                 greenMod=None,
154                 blue=None,
155                 blueOff=None,
156                 blueMod=None,
157                 gamma=None,
158                 invGamma=None
159                ):
160        self.val = val
161        self.lastClr = lastClr
162        self.tint = tint
163        self.shade = shade
164        self.comp = comp
165        self.inv = inv
166        self.gray = gray
167        self.alpha = alpha
168        self.alphaOff = alphaOff
169        self.alphaMod = alphaMod
170        self.hue = hue
171        self.hueOff = hueOff
172        self.hueMod = hueMod
173        self.sat = sat
174        self.satOff = satOff
175        self.satMod = satMod
176        self.lum = lum
177        self.lumOff = lumOff
178        self.lumMod = lumMod
179        self.red = red
180        self.redOff = redOff
181        self.redMod = redMod
182        self.green = green
183        self.greenOff = greenOff
184        self.greenMod = greenMod
185        self.blue = blue
186        self.blueOff = blueOff
187        self.blueMod = blueMod
188        self.gamma = gamma
189        self.invGamma = invGamma
190
191
192class HSLColor(Serialisable):
193
194    tagname = "hslClr"
195
196    hue = Integer()
197    sat = MinMax(min=0, max=100)
198    lum = MinMax(min=0, max=100)
199
200    #TODO add color transform options
201
202    def __init__(self,
203                 hue=None,
204                 sat=None,
205                 lum=None,
206                ):
207        self.hue = hue
208        self.sat = sat
209        self.lum = lum
210
211
212
213class RGBPercent(Serialisable):
214
215    tagname = "rgbClr"
216
217    r = MinMax(min=0, max=100)
218    g = MinMax(min=0, max=100)
219    b = MinMax(min=0, max=100)
220
221    #TODO add color transform options
222
223    def __init__(self,
224                 r=None,
225                 g=None,
226                 b=None,
227                ):
228        self.r = r
229        self.g = g
230        self.b = b
231
232
233class SchemeColor(Serialisable):
234
235    tagname = "schemeClr"
236    namespace = DRAWING_NS
237
238    tint = NestedInteger(allow_none=True)
239    shade = NestedInteger(allow_none=True)
240    comp = EmptyTag(allow_none=True)
241    inv = NestedInteger(allow_none=True)
242    gray = NestedInteger(allow_none=True)
243    alpha = NestedInteger(allow_none=True)
244    alphaOff = NestedInteger(allow_none=True)
245    alphaMod = NestedInteger(allow_none=True)
246    hue = NestedInteger(allow_none=True)
247    hueOff = NestedInteger(allow_none=True)
248    hueMod = NestedInteger(allow_none=True)
249    sat = NestedInteger(allow_none=True)
250    satOff = NestedInteger(allow_none=True)
251    satMod = NestedInteger(allow_none=True)
252    lum = NestedInteger(allow_none=True)
253    lumOff = NestedInteger(allow_none=True)
254    lumMod = NestedInteger(allow_none=True)
255    red = NestedInteger(allow_none=True)
256    redOff = NestedInteger(allow_none=True)
257    redMod = NestedInteger(allow_none=True)
258    green = NestedInteger(allow_none=True)
259    greenOff = NestedInteger(allow_none=True)
260    greenMod = NestedInteger(allow_none=True)
261    blue = NestedInteger(allow_none=True)
262    blueOff = NestedInteger(allow_none=True)
263    blueMod = NestedInteger(allow_none=True)
264    gamma = EmptyTag(allow_none=True)
265    invGamma = EmptyTag(allow_none=True)
266    val = Set(values=(['bg1', 'tx1', 'bg2', 'tx2', 'accent1', 'accent2',
267                       'accent3', 'accent4', 'accent5', 'accent6', 'hlink', 'folHlink', 'phClr',
268                       'dk1', 'lt1', 'dk2', 'lt2']))
269
270    __elements__ = ('tint', 'shade', 'comp', 'inv', 'gray', 'alpha',
271                    'alphaOff', 'alphaMod', 'hue', 'hueOff', 'hueMod', 'sat', 'satOff',
272                    'satMod', 'lum', 'lumMod', 'lumOff', 'red', 'redOff', 'redMod', 'green',
273                    'greenOff', 'greenMod', 'blue', 'blueOff', 'blueMod', 'gamma',
274                    'invGamma')
275
276    def __init__(self,
277                 tint=None,
278                 shade=None,
279                 comp=None,
280                 inv=None,
281                 gray=None,
282                 alpha=None,
283                 alphaOff=None,
284                 alphaMod=None,
285                 hue=None,
286                 hueOff=None,
287                 hueMod=None,
288                 sat=None,
289                 satOff=None,
290                 satMod=None,
291                 lum=None,
292                 lumOff=None,
293                 lumMod=None,
294                 red=None,
295                 redOff=None,
296                 redMod=None,
297                 green=None,
298                 greenOff=None,
299                 greenMod=None,
300                 blue=None,
301                 blueOff=None,
302                 blueMod=None,
303                 gamma=None,
304                 invGamma=None,
305                 val=None,
306                ):
307        self.tint = tint
308        self.shade = shade
309        self.comp = comp
310        self.inv = inv
311        self.gray = gray
312        self.alpha = alpha
313        self.alphaOff = alphaOff
314        self.alphaMod = alphaMod
315        self.hue = hue
316        self.hueOff = hueOff
317        self.hueMod = hueMod
318        self.sat = sat
319        self.satOff = satOff
320        self.satMod = satMod
321        self.lum = lum
322        self.lumOff = lumOff
323        self.lumMod = lumMod
324        self.red = red
325        self.redOff = redOff
326        self.redMod = redMod
327        self.green = green
328        self.greenOff = greenOff
329        self.greenMod = greenMod
330        self.blue = blue
331        self.blueOff = blueOff
332        self.blueMod = blueMod
333        self.gamma = gamma
334        self.invGamma = invGamma
335        self.val = val
336
337class ColorChoice(Serialisable):
338
339    tagname = "colorChoice"
340    namespace = DRAWING_NS
341
342    scrgbClr = Typed(expected_type=RGBPercent, allow_none=True)
343    RGBPercent = Alias('scrgbClr')
344    srgbClr = NestedValue(expected_type=str, allow_none=True) # needs pattern and can have transform
345    RGB = Alias('srgbClr')
346    hslClr = Typed(expected_type=HSLColor, allow_none=True)
347    sysClr = Typed(expected_type=SystemColor, allow_none=True)
348    schemeClr = Typed(expected_type=SchemeColor, allow_none=True)
349    prstClr = NestedNoneSet(values=PRESET_COLORS)
350
351    __elements__ = ('scrgbClr', 'srgbClr', 'hslClr', 'sysClr', 'schemeClr', 'prstClr')
352
353    def __init__(self,
354                 scrgbClr=None,
355                 srgbClr=None,
356                 hslClr=None,
357                 sysClr=None,
358                 schemeClr=None,
359                 prstClr=None,
360                ):
361        self.scrgbClr = scrgbClr
362        self.srgbClr = srgbClr
363        self.hslClr = hslClr
364        self.sysClr = sysClr
365        self.schemeClr = schemeClr
366        self.prstClr = prstClr
367
368_COLOR_SET = ('dk1', 'lt1', 'dk2', 'lt2', 'accent1', 'accent2', 'accent3',
369               'accent4', 'accent5', 'accent6', 'hlink', 'folHlink')
370
371
372class ColorMapping(Serialisable):
373
374    tagname = "clrMapOvr"
375
376    bg1 = Set(values=_COLOR_SET)
377    tx1 = Set(values=_COLOR_SET)
378    bg2 = Set(values=_COLOR_SET)
379    tx2 = Set(values=_COLOR_SET)
380    accent1 = Set(values=_COLOR_SET)
381    accent2 = Set(values=_COLOR_SET)
382    accent3 = Set(values=_COLOR_SET)
383    accent4 = Set(values=_COLOR_SET)
384    accent5 = Set(values=_COLOR_SET)
385    accent6 = Set(values=_COLOR_SET)
386    hlink = Set(values=_COLOR_SET)
387    folHlink = Set(values=_COLOR_SET)
388    extLst = Typed(expected_type=OfficeArtExtensionList, allow_none=True)
389
390    def __init__(self,
391                 bg1="lt1",
392                 tx1="dk1",
393                 bg2="lt2",
394                 tx2="dk2",
395                 accent1="accent1",
396                 accent2="accent2",
397                 accent3="accent3",
398                 accent4="accent4",
399                 accent5="accent5",
400                 accent6="accent6",
401                 hlink="hlink",
402                 folHlink="folHlink",
403                 extLst=None,
404                ):
405        self.bg1 = bg1
406        self.tx1 = tx1
407        self.bg2 = bg2
408        self.tx2 = tx2
409        self.accent1 = accent1
410        self.accent2 = accent2
411        self.accent3 = accent3
412        self.accent4 = accent4
413        self.accent5 = accent5
414        self.accent6 = accent6
415        self.hlink = hlink
416        self.folHlink = folHlink
417        self.extLst = extLst
418
419
420class ColorChoiceDescriptor(Typed):
421    """
422    Objects can choose from 7 different kinds of color system.
423    Assume RGBHex if a string is passed in.
424    """
425
426    expected_type = ColorChoice
427    allow_none = True
428
429    def __set__(self, instance, value):
430        if isinstance(value, str):
431            value = ColorChoice(srgbClr=value)
432        else:
433            if hasattr(self, "namespace") and value is not None:
434                value.namespace = self.namespace
435        super().__set__(instance, value)