Skip to content

Commit 8618a7d

Browse files
authored
Merge pull request #150 from kirchsth/feature/149_LegendColor
#149 Calculate missing legend colors based on brightness
2 parents a32f7d8 + 35bd190 commit 8618a7d

File tree

1 file changed

+99
-32
lines changed

1 file changed

+99
-32
lines changed

C4.puml

+99-32
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
!global $LEGEND_FONT_COLOR = "#FFFFFF"
1414
!global $LEGEND_TITLE_COLOR = "#000000"
15-
!global $LEGEND_UNDEFINED_BG_COLOR = "#D5CFEE"
16-
!global $LEGEND_UNDEFINED_FONT_COLOR = "#8B77E4"
15+
' %darken(darkkhaki,50), #khaki
16+
!global $LEGEND_DARK_COLOR = "#66622E"
17+
!global $LEGEND_LIGHT_COLOR = "#khaki"
1718

1819
' Labels
1920
' ##################################
@@ -202,7 +203,7 @@ $tagSkin
202203
!endprocedure
203204

204205
' arrow colors cannot start with # (legend background has to start with #)
205-
!function ColorNoHash($c)
206+
!function $colorWithoutHash($c)
206207
!if (%substr($c, 0, 1) == "#")
207208
!$c = %substr($c,1)
208209
!endif
@@ -213,39 +214,95 @@ $tagSkin
213214
!$elementSkin = "skinparam arrow<<" + $tagStereo + ">> {" + %newline()
214215
!$elementSkin = $elementSkin + " Color "
215216
!if ($lineColor!="")
216-
!$elementSkin = $elementSkin + ColorNoHash($lineColor)
217+
!$elementSkin = $elementSkin + $colorWithoutHash($lineColor)
217218
!endif
218219
!if ($textColor!="")
219-
!$elementSkin = $elementSkin + ";text:" + ColorNoHash($textColor)
220+
!$elementSkin = $elementSkin + ";text:" + $colorWithoutHash($textColor)
220221
!endif
221222
!$elementSkin = $elementSkin + %newline()
222223
!$elementSkin = $elementSkin + "}" + %newline()
223224
$elementSkin
224225
!endprocedure
225226

226-
!function $tagLegendEntry($tagStereo, $bgColor, $fontColor, $borderColor, $shadowing, $shape)
227-
!$tagEntry = "|"
228-
!if ($bgColor!="")
229-
!$bg = $bgColor
227+
' %is_dark() requires PlantUML version >=1.2021.6
228+
!if (%function_exists("%is_dark"))
229+
!$PlantUMLSupportsDynamicLegendColor = %true()
230+
!else
231+
!$PlantUMLSupportsDynamicLegendColor = %false()
232+
!log "dynamic undefined legend colors" requires PlantUML version >= 1.2021.6, therefore only static assigned colors are used
233+
!endif
234+
235+
!unquoted function $contrastLegend($color)
236+
!if (%is_dark($color))
237+
!$value = $LEGEND_LIGHT_COLOR
230238
!else
231-
!$bg = $LEGEND_UNDEFINED_BG_COLOR
239+
!$value = $LEGEND_DARK_COLOR
232240
!endif
233-
' named colors have to start with # too
234-
!if (%substr($bg, 0, 1) != "#")
235-
!$bg = "#" + $bg
236-
!endif
237-
!$tagEntry = $tagEntry + "<" + $bg +">"
238-
' <U+25AF> ..white rectangle
239-
!if ($borderColor!="")
240-
!$tagEntry = $tagEntry + "<color:"+$borderColor+"> <U+25AF></color> "
241+
!return $value
242+
!endfunction
243+
244+
!unquoted function $flatLegend($color)
245+
!if (%is_dark($color))
246+
!$value = $LEGEND_DARK_COLOR
241247
!else
242-
!$tagEntry = $tagEntry + "<color:"+$bg+"> <U+25AF></color> "
248+
!$value = $LEGEND_LIGHT_COLOR
243249
!endif
244-
!if ($fontColor!="")
245-
!$tagEntry = $tagEntry + "<color:"+$fontColor+">"
250+
!return $value
251+
!endfunction
252+
253+
' legend background has to start with #
254+
!function $colorWithHash($c)
255+
!if (%substr($c, 0, 1) != "#")
256+
!$c = "#" + $c
257+
!endif
258+
!return $c
259+
!endfunction
260+
261+
!function $tagLegendEntry($tagStereo, $bgColor, $fontColor, $borderColor, $shadowing, $shape)
262+
!$bg = $bgColor
263+
!$fo = $fontColor
264+
!$bo = $borderColor
265+
266+
!if ($fo == "")
267+
!if ($bg != "")
268+
!if ($PlantUMLSupportsDynamicLegendColor)
269+
!$fo = $contrastLegend($bg)
270+
!else
271+
!$fo = $LEGEND_DARK_COLOR
272+
!endif
273+
!else
274+
!if ($bo == "")
275+
!$fo = $LEGEND_DARK_COLOR
276+
!$bg = $LEGEND_LIGHT_COLOR
277+
!else
278+
!if ($PlantUMLSupportsDynamicLegendColor)
279+
!$fo = $flatLegend($bo)
280+
!$bg = $contrastLegend($bo)
281+
!else
282+
!$fo = $LEGEND_DARK_COLOR
283+
!$bg = $LEGEND_LIGHT_COLOR
284+
!endif
285+
!endif
286+
!endif
246287
!else
247-
!$tagEntry = $tagEntry + "<color:"+$LEGEND_UNDEFINED_FONT_COLOR+">"
288+
!if ($bg == "")
289+
!if ($PlantUMLSupportsDynamicLegendColor)
290+
!$bg = $contrastLegend($fo)
291+
!else
292+
!$bg = $LEGEND_LIGHT_COLOR
293+
!endif
294+
!endif
295+
!endif
296+
297+
!if ($bo == "")
298+
!$bo = $bg
248299
!endif
300+
301+
!$tagEntry = "|"
302+
!$tagEntry = $tagEntry + "<" + $colorWithHash($bg) +">"
303+
' <U+25AF> ..white rectangle
304+
!$tagEntry = $tagEntry + "<color:"+$bo+"> <U+25AF></color> "
305+
!$tagEntry = $tagEntry + "<color:"+$fo+">"
249306
!$tagEntry = $tagEntry + " " + $tagStereo + " "
250307
!if ($shadowing == "true")
251308
!$tagEntry = $tagEntry + $LEGEND_SHADOW_TEXT
@@ -275,18 +332,28 @@ $elementSkin
275332
!endfunction
276333

277334
!function $tagRelLegendEntry($tagStereo, $textColor, $lineColor)
278-
!$tagEntry = "|"
279-
' <U+2500> ..white line
280-
!if ($lineColor!="")
281-
!$tagEntry = $tagEntry + "<color:"+$lineColor+"> <U+2500></color> "
282-
!else
283-
!$tagEntry = $tagEntry + " <U+2500> "
335+
!$tc = $textColor
336+
!$lc = $lineColor
337+
338+
!if ($tc=="")
339+
!if ($PlantUMLSupportsDynamicLegendColor)
340+
!$tc = $flatLegend($ARROW_COLOR)
341+
!else
342+
!$tc = $LEGEND_DARK_COLOR
343+
!endif
284344
!endif
285-
!if ($textColor!="")
286-
!$tagEntry = $tagEntry + "<color:"+$textColor+">"
287-
!else
288-
!$tagEntry = $tagEntry + "<color:"+$LEGEND_UNDEFINED_FONT_COLOR+">"
345+
!if ($lc=="")
346+
!if ($PlantUMLSupportsDynamicLegendColor)
347+
!$lc = $flatLegend($ARROW_COLOR)
348+
!else
349+
!$lc = $LEGEND_DARK_COLOR
350+
!endif
289351
!endif
352+
353+
!$tagEntry = "|"
354+
' <U+2500> ..white line
355+
!$tagEntry = $tagEntry + "<color:"+$lc+"> <U+2500></color> "
356+
!$tagEntry = $tagEntry + "<color:"+$tc+">"
290357
!$tagEntry = $tagEntry + " " + $tagStereo + " "
291358
!if ($textColor == "")
292359
!$tagEntry = $tagEntry + $LEGEND_NO_FONT_TEXT

0 commit comments

Comments
 (0)