summaryrefslogtreecommitdiff
path: root/xslt/common/color.xsl
blob: 1cec3ba89c2fc6029ce3a9b32ef950fb7df3a222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
<?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- -->
<!--
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License
along with this program; see the file COPYING.LGPL.  If not, see <http://www.gnu.org/licenses/>.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:math="http://exslt.org/math"
                exclude-result-prefixes="math"
                version="1.0">

<!--!!==========================================================================
Colors
Common named colors and color utilities for output styling.
@revision[version=3.28 date=2016-01-03 status=final]

This stylesheet provides a common interface to specify custom colors for
transformations to presentation-oreinted formats. This allows similar
output for different types of input documents.

This stylesheet also provides a number of templates for manipulating colors
and extracting information about colors.
-->


<!--**==========================================================================
color.hex2dec
Convert a hexidecimal number to decimal.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$hex: The hexidecimal number to convert to decimal.

This template converts a hexidecimal number to decimal. It's useful for getting
the numeric values of color components in a hexidecimal color code.
-->
<xsl:template name="color.hex2dec">
  <xsl:param name="hex"/>
  <xsl:variable name="char" select="substring($hex, string-length($hex))"/>
  <xsl:variable name="dec">
    <xsl:choose>
      <xsl:when test="$char = 'a' or $char = 'A'">10</xsl:when>
      <xsl:when test="$char = 'b' or $char = 'B'">11</xsl:when>
      <xsl:when test="$char = 'c' or $char = 'C'">12</xsl:when>
      <xsl:when test="$char = 'd' or $char = 'D'">13</xsl:when>
      <xsl:when test="$char = 'e' or $char = 'E'">14</xsl:when>
      <xsl:when test="$char = 'f' or $char = 'F'">15</xsl:when>
      <xsl:otherwise><xsl:value-of select="$char"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="string-length($hex) = 1">
      <xsl:value-of select="number($dec)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="tens">
        <xsl:call-template name="color.hex2dec">
          <xsl:with-param name="hex" select="substring($hex, 1, string-length($hex) - 1)"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$dec + 16 * $tens"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
color.r
Extract the red component of a color.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$color: A color specified in hexidecimal, `rgb()`, or `rgba()`.

This template extracts the red portion of a color, returning a number between
0 and 255. It accepts six-digit and three-digit hexidecimal color codes,
colors specified with `rgb()`, and colors specified with `rgba()`. It does
not accept HSL or named HTML colors.
-->
<xsl:template name="color.r">
  <xsl:param name="color"/>
  <xsl:choose>
    <xsl:when test="starts-with($color, '#') and string-length($color) = 7">
      <xsl:call-template name="color.hex2dec">
        <xsl:with-param name="hex" select="substring($color, 2, 2)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="starts-with($color, '#') and string-length($color) = 4">
      <xsl:call-template name="color.hex2dec">
        <xsl:with-param name="hex" select="concat(substring($color, 2, 1), substring($color, 2, 1))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="starts-with($color, 'rgb(') or starts-with($color, 'rgba(')">
      <xsl:value-of select="substring-before(substring-after($color, '('), ',')"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
color.g
Extract the green component of a color.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$color: A color specified in hexidecimal, `rgb()`, or `rgba()`.

This template extracts the green portion of a color, returning a number between
0 and 255. It accepts six-digit and three-digit hexidecimal color codes,
colors specified with `rgb()`, and colors specified with `rgba()`. It does
not accept HSL or named HTML colors.
-->
<xsl:template name="color.g">
  <xsl:param name="color"/>
  <xsl:choose>
    <xsl:when test="starts-with($color, '#') and string-length($color) = 7">
      <xsl:call-template name="color.hex2dec">
        <xsl:with-param name="hex" select="substring($color, 4, 2)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="starts-with($color, '#') and string-length($color) = 4">
      <xsl:call-template name="color.hex2dec">
        <xsl:with-param name="hex" select="concat(substring($color, 3, 1), substring($color, 3, 1))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="starts-with($color, 'rgb(') or starts-with($color, 'rgba(')">
      <xsl:value-of select="substring-before(substring-after($color, ','), ',')"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
color.b
Extract the blue component of a color.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$color: A color specified in hexidecimal, `rgb()`, or `rgba()`.

This template extracts the blue portion of a color, returning a number between
0 and 255. It accepts six-digit and three-digit hexidecimal color codes,
colors specified with `rgb()`, and colors specified with `rgba()`. It does
not accept HSL or named HTML colors.
-->
<xsl:template name="color.b">
  <xsl:param name="color"/>
  <xsl:choose>
    <xsl:when test="starts-with($color, '#') and string-length($color) = 7">
      <xsl:call-template name="color.hex2dec">
        <xsl:with-param name="hex" select="substring($color, 6, 2)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="starts-with($color, '#') and string-length($color) = 4">
      <xsl:call-template name="color.hex2dec">
        <xsl:with-param name="hex" select="concat(substring($color, 4, 1), substring($color, 4, 1))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="starts-with($color, 'rgb(')">
      <xsl:value-of select="substring-before(
                              substring-after(substring-after($color, ','), ','),
                              ')')"/>
    </xsl:when>
    <xsl:when test="starts-with($color, 'rgba(')">
      <xsl:value-of select="substring-before(
                              substring-after(substring-after($color, ','), ','),
                              ',')"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
color.a
Extract the alpha value of a color.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$color: A color specified in hexidecimal, `rgb()`, or `rgba()`.

This template extracts the alpha, or opacity level, of a color. It returns a
number between 0.0 and 1.0. It accepts six-digit and three-digit hexidecimal
color codes, colors specified with `rgb()`, and colors specified with
`rgba()`. It does not accept HSL or named HTML colors. For colors specified
with anything other than `rgba()`, it always returns 1.0.
-->
<xsl:template name="color.a">
  <xsl:param name="color"/>
  <xsl:choose>
    <xsl:when test="starts-with($color, 'rgba(')">
      <xsl:value-of select="number(substring-before(substring-after(
                            substring-after(substring-after($color, ','), ','), ','), ')'))"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>1.0</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
color.rl
Get the relative luminance of a color.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$color: A color specified in hexidecimal, `rgb()`, or `rgba()`.

This template calculates the relative luminance of a color, returning a number
between 0.0 and 1.0. The relative luminance is used when calculating color
contrast. The relative luminance algorithm is defined by the WCAG:

$link[>>http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef]

This template accepts six-digit and three-digit hexidecimal color codes, colors
specified with `rgb()`, and colors specified with `rgba()`. It does not accept
HSL or named HTML colors.
-->
<xsl:template name="color.rl">
  <xsl:param name="color"/>
  <xsl:variable name="r">
    <xsl:variable name="rsrgb_">
      <xsl:call-template name="color.r">
        <xsl:with-param name="color" select="$color"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="rsrgb" select="$rsrgb_ div 255"/>
    <xsl:choose>
      <xsl:when test="$rsrgb &gt; 0.03928">
        <xsl:value-of select="math:power(($rsrgb + 0.055) div 1.055, 2.4)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$rsrgb div 12.92"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="g">
    <xsl:variable name="gsrgb_">
      <xsl:call-template name="color.g">
        <xsl:with-param name="color" select="$color"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="gsrgb" select="$gsrgb_ div 255"/>
    <xsl:choose>
      <xsl:when test="$gsrgb &gt; 0.03928">
        <xsl:value-of select="math:power(($gsrgb + 0.055) div 1.055, 2.4)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$gsrgb div 12.92"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable> 
  <xsl:variable name="b">
    <xsl:variable name="bsrgb_">
      <xsl:call-template name="color.b">
        <xsl:with-param name="color" select="$color"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="bsrgb" select="$bsrgb_ div 255"/>
    <xsl:choose>
      <xsl:when test="$bsrgb &gt; 0.03928">
        <xsl:value-of select="math:power(($bsrgb + 0.055) div 1.055, 2.4)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$bsrgb div 12.92"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable> 
  <xsl:value-of select="(0.2126 * $r) + (0.0722 * $b) + (0.7152 * $g)"/>
</xsl:template>


<!--**==========================================================================
color.contrast
Get the contrast between two colors.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$bg: A color specified in hexidecimal, `rgb()`, or `rgba()`.
$fg: A color specified in hexidecimal, `rgb()`, or `rgba()`.

This template calculates the contrast ratio between colors. The contrast ratio
is a number between 1 and 21. The algorithm is defined by the WCAG:

$link[>>http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef]

This template calls {color.rl} to get the relative luminance of $bg and $fg.
It accepts six-digit and three-digit hexidecimal color codes, colors specified
with `rgb()`, and colors specified with `rgba()`. It does not accept HSL or
named HTML colors. Note that it does not take the alpha value into account when
calculating the contrast ratio. Semi-transparent colors will have a lower
actual contrast ratio than what is reported by this template.

The WCAG recommends a contrast ratio of at least 4.5 for normal text, and a
ratio of at least 3.0 for large-scale text.
-->
<xsl:template name="color.contrast">
  <xsl:param name="bg"/>
  <xsl:param name="fg"/>
  <xsl:variable name="bg.rl">
    <xsl:call-template name="color.rl">
      <xsl:with-param name="color" select="$bg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="fg.rl">
    <xsl:call-template name="color.rl">
      <xsl:with-param name="color" select="$fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$bg.rl > $fg.rl">
      <xsl:value-of select="round(100 * (($bg.rl + 0.05) div ($fg.rl + 0.05))) div 100"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="round(100 * (($fg.rl + 0.05) div ($bg.rl + 0.05))) div 100"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
color.blend
Blend two colors together at a specified mix level.
@revision[version=3.28 date=2016-01-03 status=final]

[xsl:params]
$bg: A color specified in hexidecimal, `rgb()`, or `rgba()`.
$fg: A color specified in hexidecimal, `rgb()`, or `rgba()`.
$mix: The mix level, between 0.0 and 1.0.

This template calculates a color by selecting a midpoint between $bg and
$fg, where the $mix parameter specifies how far to move from $bg to $fg
For opaque colors, setting $mix to 0.0 will result in $bg, and setting
$mix to 1.0 will result in $fg.

This template takes the alpha values of $bg and $fg into account, so
that specifying 1.0 for $mix will result in a color that is the result
of overlaying $fg on top of $bg. In effect, $mix acts as a multiplier
on the alpha channels of the colors.

This template calls {color.r}, {color.g}, {color.b}, and {color.a} to get
the components of $bg and $fg. It accepts six-digit and three-digit
hexidecimal color codes, colors specified with `rgb()`, and colors specified
with `rgba()`. It does not accept HSL or named HTML colors.

If the return color is fully opaque, this template returns the color using
the `rgb()` scheme. Otherwise, it uses the `rgba()` scheme.
-->
<xsl:template name="color.blend">
  <xsl:param name="bg" select="'#ffffff'"/>
  <xsl:param name="fg" select="'#000000'"/>
  <xsl:param name="mix" select="0.5"/>

  <xsl:variable name="fgr">
    <xsl:call-template name="color.r">
      <xsl:with-param name="color" select="$fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="fgb">
    <xsl:call-template name="color.b">
      <xsl:with-param name="color" select="$fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="fgg">
    <xsl:call-template name="color.g">
      <xsl:with-param name="color" select="$fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="fga">
    <xsl:call-template name="color.a">
      <xsl:with-param name="color" select="$fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="bgr">
    <xsl:call-template name="color.r">
      <xsl:with-param name="color" select="$bg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="bgb">
    <xsl:call-template name="color.b">
      <xsl:with-param name="color" select="$bg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="bgg">
    <xsl:call-template name="color.g">
      <xsl:with-param name="color" select="$bg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="bga">
    <xsl:call-template name="color.a">
      <xsl:with-param name="color" select="$bg"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="a" select="($fga * $mix) + ($bga * (1 - $mix))"/>
  <xsl:variable name="r" select="(($bgr * $bga * (1 - $mix)) +
                                  ($fgr * $fga * $mix)) div $a"/>
  <xsl:variable name="g" select="(($bgg * $bga * (1 - $mix)) +
                                  ($fgg * $fga * $mix)) div $a"/>
  <xsl:variable name="b" select="(($bgb * $bga * (1 - $mix)) +
                                  ($fgb * $fga * $mix)) div $a"/>
  <xsl:variable name="rgba" select="$a &lt; 1"/>
  <xsl:choose>
    <xsl:when test="$rgba">
      <xsl:text>rgba(</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>rgb(</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:choose>
    <xsl:when test="$r &lt; 0">
      <xsl:text>0</xsl:text>
    </xsl:when>
    <xsl:when test="$r &gt; 255">
      <xsl:text>255</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="round($r)"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:text>,</xsl:text>
  <xsl:choose>
    <xsl:when test="$g &lt; 0">
      <xsl:text>0</xsl:text>
    </xsl:when>
    <xsl:when test="$g &gt; 255">
      <xsl:text>255</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="round($g)"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:text>,</xsl:text>
  <xsl:choose>
    <xsl:when test="$b &lt; 0">
      <xsl:text>0</xsl:text>
    </xsl:when>
    <xsl:when test="$b &gt; 255">
      <xsl:text>255</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="round($b)"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="$rgba">
    <xsl:text>,</xsl:text>
    <xsl:choose>
      <xsl:when test="$a &lt; 0">
        <xsl:text>0</xsl:text>
      </xsl:when>
      <xsl:when test="$a &gt; 1.0">
        <xsl:text>1.0</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$a"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
  <xsl:text>)</xsl:text>
</xsl:template>


<!--#* _color.fgify -->
<xsl:template name="_color.fgify">
  <xsl:param name="color"/>
  <xsl:param name="target" select="5"/>
  <xsl:param name="recdepth" select="0"/>
  <xsl:param name="name"/>
  <xsl:variable name="newcolor">
    <xsl:call-template name="color.blend">
      <xsl:with-param name="bg" select="$color"/>
      <xsl:with-param name="fg" select="$color.fg"/>
      <xsl:with-param name="mix" select="0.1"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="contrast">
    <xsl:call-template name="color.contrast">
      <xsl:with-param name="bg" select="$color.bg"/>
      <xsl:with-param name="fg" select="$newcolor"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$contrast >= $target">
      <xsl:value-of select="$newcolor"/>
    </xsl:when>
    <xsl:when test="$recdepth > 10">
      <xsl:message>
        <xsl:text>Recursion depth exceeded calculating </xsl:text>
        <xsl:if test="$name">
          <xsl:value-of select="concat($name, ' ')"/>
        </xsl:if>
        <xsl:text>fg color. Using </xsl:text>
        <xsl:value-of select="$newcolor"/>
      </xsl:message>
      <xsl:value-of select="$newcolor"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="_color.fgify">
        <xsl:with-param name="color" select="$newcolor"/>
        <xsl:with-param name="target" select="$target"/>
        <xsl:with-param name="recdepth" select="$recdepth + 1"/>
        <xsl:with-param name="name" select="$name"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--#@ color.background -->
<xsl:variable name="_color.isinverse">
  <xsl:variable name="contrast">
    <xsl:call-template name="color.contrast">
      <xsl:with-param name="bg" select="'#000000'"/>
      <xsl:with-param name="fg" select="$color.fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$contrast > 5">
      <xsl:text>1</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>0</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>


<!--#* _color.bgify -->
<xsl:template name="_color.bgify">
  <xsl:param name="color"/>
  <xsl:param name="target">
    <xsl:choose>
      <xsl:when test="normalize-space($_color.isinverse) = '1'">
        <xsl:value-of select="11"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="19"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:param>
  <xsl:param name="recdepth" select="0"/>
  <xsl:param name="name"/>
  <xsl:variable name="newcolor">
    <xsl:call-template name="color.blend">
      <xsl:with-param name="bg" select="$color"/>
      <xsl:with-param name="fg" select="$color.bg"/>
      <xsl:with-param name="mix" select="0.2"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="contrast">
    <xsl:call-template name="color.contrast">
      <xsl:with-param name="bg" select="$newcolor"/>
      <xsl:with-param name="fg" select="$color.fg"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$contrast >= $target">
      <xsl:value-of select="$newcolor"/>
    </xsl:when>
    <xsl:when test="$recdepth > 20">
      <xsl:message>
        <xsl:text>Recursion depth exceeded calculating </xsl:text>
        <xsl:if test="$name">
          <xsl:value-of select="concat($name, ' ')"/>
        </xsl:if>
        <xsl:text>bg color. Using </xsl:text>
        <xsl:value-of select="$newcolor"/>
      </xsl:message>
      <xsl:value-of select="$newcolor"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="_color.bgify">
        <xsl:with-param name="color" select="$newcolor"/>
        <xsl:with-param name="target" select="$target"/>
        <xsl:with-param name="recdepth" select="$recdepth + 1"/>
        <xsl:with-param name="name" select="$name"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>



<!--@@==========================================================================
color.fg
The primary text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameters specifies the normal color of text. It should have a high color
contrast against {color.bg}. Other text colors can be automatically computed
based on this color.
-->
<xsl:param name="color.fg" select="'#000000'"/>


<!--@@==========================================================================
color.bg
The normal background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameters specifies the background color. It should have a high color
contrast against {color.fg}. Other background colors can be automatically
computed based on this color.
-->
<xsl:param name="color.bg" select="'#ffffff'"/>


<!--@@==========================================================================
color.red
A red accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of red that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.red" select="'#c01c28'"/>


<!--@@==========================================================================
color.fg.red
A red text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of red that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.red} and {color.fg}.
-->
<xsl:param name="color.fg.red">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.red"/>
    <xsl:with-param name="name" select="'red'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.red
A red background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of red that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.red} and {color.bg}.
-->
<xsl:param name="color.bg.red">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.red"/>
    <xsl:with-param name="name" select="'red'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.orange
An orange accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of orange that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.orange" select="'#ffa348'"/>


<!--@@==========================================================================
color.fg.orange
An orange text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of orange that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.orange} and {color.fg}.
-->
<xsl:param name="color.fg.orange">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.orange"/>
    <xsl:with-param name="name" select="'orange'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.orange
An orange background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of orange that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.orange} and {color.bg}.
-->
<xsl:param name="color.bg.orange">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.orange"/>
    <xsl:with-param name="name" select="'orange'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.yellow
A yellow accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of yellow that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.yellow" select="'#f8e45c'"/>


<!--@@==========================================================================
color.fg.yellow
A yellow text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of yellow that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.yellow} and {color.fg}.
-->
<xsl:param name="color.fg.yellow">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.yellow"/>
    <xsl:with-param name="target" select="5.5"/>
    <xsl:with-param name="name" select="'yellow'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.yellow
A yellow background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of yellow that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.yellow} and {color.bg}.
-->
<xsl:param name="color.bg.yellow">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.yellow"/>
    <xsl:with-param name="target">
      <xsl:choose>
        <xsl:when test="normalize-space($_color.isinverse) = '1'">
          <xsl:value-of select="9"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="20"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:with-param>
    <xsl:with-param name="name" select="'yellow'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.green
A green accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of green that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.green" select="'#57e389'"/>


<!--@@==========================================================================
color.fg.green
A green text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of green that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.green} and {color.fg}.
-->
<xsl:param name="color.fg.green">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.green"/>
    <xsl:with-param name="name" select="'green'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.green
A green background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of green that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.green} and {color.bg}.
-->
<xsl:param name="color.bg.green">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.green"/>
    <xsl:with-param name="name" select="'green'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.blue
A blue accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of blue that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.blue" select="'#3584e4'"/>


<!--@@==========================================================================
color.fg.blue
A blue text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of blue that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.blue} and {color.fg}.
-->
<xsl:param name="color.fg.blue">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.blue"/>
    <xsl:with-param name="name" select="'blue'"/>
  </xsl:call-template>
</xsl:param>

<!--@@==========================================================================
color.bg.blue
A blue background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of blue that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.blue} and {color.bg}.
-->
<xsl:param name="color.bg.blue">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.blue"/>
    <xsl:with-param name="name" select="'blue'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.purple
A purple accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of purple that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.purple" select="'#a347ba'"/>


<!--@@==========================================================================
color.fg.purple
A purple text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of purple that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.purple} and {color.fg}.
-->
<xsl:param name="color.fg.purple">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.purple"/>
    <xsl:with-param name="name" select="'purple'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.purple
A purple background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of purple that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.purple} and {color.bg}.
-->
<xsl:param name="color.bg.purple">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.purple"/>
    <xsl:with-param name="name" select="'purple'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.gray
A gray accent color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of gray that is suitable for borders and
other accents. It should have some contrast against background colors, but it
does not need as high of a contrast as text colors.
-->
<xsl:param name="color.gray" select="'#c0bfbc'"/>


<!--@@==========================================================================
color.fg.gray
A gray text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of gray that is suitable for text. It should
have a high color contrast against {color.bg}. If not specified, it can be
automatically computed based on {color.gray} and {color.fg}.
-->
<xsl:param name="color.fg.gray">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.gray"/>
    <xsl:with-param name="name" select="'gray'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.gray
A gray background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of gray that is suitable for backgrounds.
It should have a high color contrast against {color.fg}. If not specified,
it can be automatically computed based on {color.gray} and {color.bg}.
-->
<xsl:param name="color.bg.gray">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.gray"/>
    <xsl:with-param name="name" select="'gray'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.fg.dark
A dark gray text color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a dark shade of gray that is suitable for text. It
should have a very high color contrast against {color.bg}. It is typically
used to slightly reduce the visual weight of headers and bold text. If not
specified, it can be automatically computed based on {color.gray} and
{color.fg}.
-->
<xsl:param name="color.fg.dark">
  <xsl:call-template name="_color.fgify">
    <xsl:with-param name="color" select="$color.gray"/>
    <xsl:with-param name="target" select="8"/>
    <xsl:with-param name="name" select="'dark'"/>
  </xsl:call-template>
</xsl:param>


<!--@@==========================================================================
color.bg.dark
A dark gray background color.
@revision[version=3.28 date=2016-01-03 status=final]

This parameter specifies a shade of gray that is suitable for backgrounds,
and is darker than {color.bg.gray}. It should have a high color contrast
against {color.fg}. It is typically used at the intersection of shaded
rows and columns in a table, or as a very light gray accent color. If not
specified, it can be automatically computed based on {color.gray} and
{color.bg}.
-->
<xsl:param name="color.bg.dark">
  <xsl:call-template name="_color.bgify">
    <xsl:with-param name="color" select="$color.gray"/>
    <xsl:with-param name="target">
      <xsl:choose>
        <xsl:when test="normalize-space($_color.isinverse) = '1'">
          <xsl:value-of select="9"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="17"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:with-param>
    <xsl:with-param name="name" select="'dark'"/>
  </xsl:call-template>
</xsl:param>


<!--
Old color parameters below. Kept for compatibility with existing
customizations that reference these colors.
-->

<!--#@ color.background -->
<xsl:param name="color.background" select="$color.bg"/>

<!--#@ color.link -->
<xsl:param name="color.link" select="$color.fg.blue"/>

<!--#@ color.link_visited -->
<xsl:param name="color.link_visited" select="$color.fg.purple"/>

<!--#@ color.text -->
<xsl:param name="color.text" select="$color.fg"/>

<!--#@ color.text_light -->
<xsl:param name="color.text_light" select="$color.fg.dark"/>

<!--#@ color.text_error -->
<xsl:param name="color.text_error" select="$color.fg.red"/>

<!--#@ color.blue_background -->
<xsl:param name="color.blue_background" select="$color.bg.blue"/>

<!--#@ color.blue_border -->
<xsl:param name="color.blue_border" select="$color.blue"/>

<!--#@ color.gray_background -->
<xsl:param name="color.gray_background" select="$color.bg.gray"/>

<!--#@ color.dark_background -->
<xsl:param name="color.dark_background" select="$color.bg.dark"/>

<!--#@ color.gray_border -->
<xsl:param name="color.gray_border" select="$color.gray"/>

<!--#@ color.red_background -->
<xsl:param name="color.red_background" select="$color.bg.red"/>

<!--#@ color.red_border -->
<xsl:param name="color.red_border" select="$color.red"/>

<!--#@ color.yellow_background -->
<xsl:param name="color.yellow_background" select="$color.bg.yellow"/>

<!--#@ color.yellow_border -->
<xsl:param name="color.yellow_border" select="$color.yellow"/>


</xsl:stylesheet>