1:- module(colours, [colour//1,
    2                    has_alpha/1,
    3                    colour_with_alpha/3,
    4                    as_transparent/2,
    5                    colour_css/2]).

CSS Colours

DCGs and helper predicates for parsing and coverting colours.

author
- James Cash */
   13:- use_module(library(dcg/basics), [xdigits//1, number//1]).   14:- use_module(library(dcg/high_order), [optional//2]).   15
   16:- discontiguous colour//1.   17
   18% pre-defined colours
   19
   20term_expansion(predefined_colour(Name, RGB), DcgDef) :-
   21    string_codes(Name, NameCodes),
   22    hex_bytes(RGB, [R, G, B]),
   23    expand_term((predef_colour(rgb(R, G, B)) --> NameCodes, !),
   24                DcgDef).
   25
   26predefined_colour("white", "ffffff").
   27predefined_colour("black", "000000").
   28
   29predefined_colour("rose-100", "ffe4e6").
   30predefined_colour("rose-200", "fecdd3").
   31predefined_colour("rose-300", "fda4af").
   32predefined_colour("rose-400", "fb7185").
   33predefined_colour("rose-500", "f43f5e").
   34predefined_colour("rose-600", "e11d48").
   35predefined_colour("rose-700", "be123c").
   36predefined_colour("rose-800", "9f1239").
   37predefined_colour("rose-900", "881337").
   38predefined_colour("rose-50", "fff1f2").
   39
   40predefined_colour("pink-100", "fce7f3").
   41predefined_colour("pink-200", "fbcfe8").
   42predefined_colour("pink-300", "f9a8d4").
   43predefined_colour("pink-400", "f472b6").
   44predefined_colour("pink-500", "ec4899").
   45predefined_colour("pink-600", "db2777").
   46predefined_colour("pink-700", "be185d").
   47predefined_colour("pink-800", "9d174d").
   48predefined_colour("pink-900", "831843").
   49predefined_colour("pink-50", "fdf2f8").
   50
   51predefined_colour("fuchsia-100", "fae8ff").
   52predefined_colour("fuchsia-200", "f5d0fe").
   53predefined_colour("fuchsia-300", "f0abfc").
   54predefined_colour("fuchsia-400", "e879f9").
   55predefined_colour("fuchsia-500", "d946ef").
   56predefined_colour("fuchsia-600", "c026d3").
   57predefined_colour("fuchsia-700", "a21caf").
   58predefined_colour("fuchsia-800", "86198f").
   59predefined_colour("fuchsia-900", "701a75").
   60predefined_colour("fuchsia-50", "fdf4ff").
   61
   62predefined_colour("purple-100", "f3e8ff").
   63predefined_colour("purple-200", "e9d5ff").
   64predefined_colour("purple-300", "d8b4fe").
   65predefined_colour("purple-400", "c084fc").
   66predefined_colour("purple-500", "a855f7").
   67predefined_colour("purple-600", "9333ea").
   68predefined_colour("purple-700", "7e22ce").
   69predefined_colour("purple-800", "6b21a8").
   70predefined_colour("purple-900", "581c87").
   71predefined_colour("purple-50", "faf5ff").
   72
   73predefined_colour("violet-100", "ede9fe").
   74predefined_colour("violet-200", "ddd6fe").
   75predefined_colour("violet-300", "c4b5fd").
   76predefined_colour("violet-400", "a78bfa").
   77predefined_colour("violet-500", "8b5cf6").
   78predefined_colour("violet-600", "7c3aed").
   79predefined_colour("violet-700", "6d28d9").
   80predefined_colour("violet-800", "5b21b6").
   81predefined_colour("violet-900", "4c1d95").
   82predefined_colour("violet-50", "f5f3ff").
   83
   84predefined_colour("indigo-100", "e0e7ff").
   85predefined_colour("indigo-200", "c7d2fe").
   86predefined_colour("indigo-300", "a5b4fc").
   87predefined_colour("indigo-400", "818cf8").
   88predefined_colour("indigo-500", "6366f1").
   89predefined_colour("indigo-600", "4f46e5").
   90predefined_colour("indigo-700", "4338ca").
   91predefined_colour("indigo-800", "3730a3").
   92predefined_colour("indigo-900", "312e81").
   93predefined_colour("indigo-50", "eef2ff").
   94
   95predefined_colour("blue-100", "dbeafe").
   96predefined_colour("blue-200", "bfdbfe").
   97predefined_colour("blue-300", "93c5fd").
   98predefined_colour("blue-400", "60a5fa").
   99predefined_colour("blue-500", "3b82f6").
  100predefined_colour("blue-600", "2563eb").
  101predefined_colour("blue-700", "1d4ed8").
  102predefined_colour("blue-800", "1e40af").
  103predefined_colour("blue-900", "1e3a8a").
  104predefined_colour("blue-50", "eff6ff").
  105
  106predefined_colour("lightBlue-100", "e0f2fe").
  107predefined_colour("lightBlue-200", "bae6fd").
  108predefined_colour("lightBlue-300", "7dd3fc").
  109predefined_colour("lightBlue-400", "38bdf8").
  110predefined_colour("lightBlue-500", "0ea5e9").
  111predefined_colour("lightBlue-600", "0284c7").
  112predefined_colour("lightBlue-700", "0369a1").
  113predefined_colour("lightBlue-800", "075985").
  114predefined_colour("lightBlue-900", "0c4a6e").
  115predefined_colour("lightBlue-50", "f0f9ff").
  116
  117predefined_colour("cyan-100", "cffafe").
  118predefined_colour("cyan-200", "a5f3fc").
  119predefined_colour("cyan-300", "67e8f9").
  120predefined_colour("cyan-400", "22d3ee").
  121predefined_colour("cyan-500", "06b6d4").
  122predefined_colour("cyan-600", "0891b2").
  123predefined_colour("cyan-700", "0e7490").
  124predefined_colour("cyan-800", "155e75").
  125predefined_colour("cyan-900", "164e63").
  126predefined_colour("cyan-50", "ecfeff").
  127
  128predefined_colour("teal-100", "ccfbf1").
  129predefined_colour("teal-200", "99f6e4").
  130predefined_colour("teal-300", "5eead4").
  131predefined_colour("teal-400", "2dd4bf").
  132predefined_colour("teal-500", "14b8a6").
  133predefined_colour("teal-600", "0d9488").
  134predefined_colour("teal-700", "0f766e").
  135predefined_colour("teal-800", "115e59").
  136predefined_colour("teal-900", "134e4a").
  137predefined_colour("teal-50", "f0fdfa").
  138
  139predefined_colour("emerald-100", "d1fae5").
  140predefined_colour("emerald-200", "a7f3d0").
  141predefined_colour("emerald-300", "6ee7b7").
  142predefined_colour("emerald-400", "34d399").
  143predefined_colour("emerald-500", "10b981").
  144predefined_colour("emerald-600", "059669").
  145predefined_colour("emerald-700", "047857").
  146predefined_colour("emerald-800", "065f46").
  147predefined_colour("emerald-900", "064e3b").
  148predefined_colour("emerald-50", "ecfdf5").
  149
  150predefined_colour("green-100", "dcfce7").
  151predefined_colour("green-200", "bbf7d0").
  152predefined_colour("green-300", "86efac").
  153predefined_colour("green-400", "4ade80").
  154predefined_colour("green-500", "22c55e").
  155predefined_colour("green-600", "16a34a").
  156predefined_colour("green-700", "15803d").
  157predefined_colour("green-800", "166534").
  158predefined_colour("green-900", "14532d").
  159predefined_colour("green-50", "f0fdf4").
  160
  161predefined_colour("lime-100", "ecfccb").
  162predefined_colour("lime-200", "d9f99d").
  163predefined_colour("lime-300", "bef264").
  164predefined_colour("lime-400", "a3e635").
  165predefined_colour("lime-500", "84cc16").
  166predefined_colour("lime-600", "65a30d").
  167predefined_colour("lime-700", "4d7c0f").
  168predefined_colour("lime-800", "3f6212").
  169predefined_colour("lime-900", "365314").
  170predefined_colour("lime-50", "f7fee7").
  171
  172predefined_colour("yellow-100", "fef9c3").
  173predefined_colour("yellow-200", "fef08a").
  174predefined_colour("yellow-300", "fde047").
  175predefined_colour("yellow-400", "facc15").
  176predefined_colour("yellow-500", "eab308").
  177predefined_colour("yellow-600", "ca8a04").
  178predefined_colour("yellow-700", "a16207").
  179predefined_colour("yellow-800", "854d0e").
  180predefined_colour("yellow-900", "713f12").
  181predefined_colour("yellow-50", "fefce8").
  182
  183predefined_colour("amber-100", "fef3c7").
  184predefined_colour("amber-200", "fde68a").
  185predefined_colour("amber-300", "fcd34d").
  186predefined_colour("amber-400", "fbbf24").
  187predefined_colour("amber-500", "f59e0b").
  188predefined_colour("amber-600", "d97706").
  189predefined_colour("amber-700", "b45309").
  190predefined_colour("amber-800", "92400e").
  191predefined_colour("amber-900", "78350f").
  192predefined_colour("amber-50", "fffbeb").
  193
  194predefined_colour("orange-100", "ffedd5").
  195predefined_colour("orange-200", "fed7aa").
  196predefined_colour("orange-300", "fdba74").
  197predefined_colour("orange-400", "fb923c").
  198predefined_colour("orange-500", "f97316").
  199predefined_colour("orange-600", "ea580c").
  200predefined_colour("orange-700", "c2410c").
  201predefined_colour("orange-800", "9a3412").
  202predefined_colour("orange-900", "7c2d12").
  203predefined_colour("orange-50", "fff7ed").
  204
  205predefined_colour("red-100", "fee2e2").
  206predefined_colour("red-200", "fecaca").
  207predefined_colour("red-300", "fca5a5").
  208predefined_colour("red-400", "f87171").
  209predefined_colour("red-500", "ef4444").
  210predefined_colour("red-600", "dc2626").
  211predefined_colour("red-700", "b91c1c").
  212predefined_colour("red-800", "991b1b").
  213predefined_colour("red-900", "7f1d1d").
  214predefined_colour("red-50", "fef2f2").
  215
  216predefined_colour("warmGray-100", "f5f5f4").
  217predefined_colour("warmGray-200", "e7e5e4").
  218predefined_colour("warmGray-300", "d6d3d1").
  219predefined_colour("warmGray-400", "a8a29e").
  220predefined_colour("warmGray-500", "78716c").
  221predefined_colour("warmGray-600", "57534e").
  222predefined_colour("warmGray-700", "44403c").
  223predefined_colour("warmGray-800", "292524").
  224predefined_colour("warmGray-900", "1c1917").
  225predefined_colour("warmGray-50", "fafaf9").
  226
  227predefined_colour("trueGray-100", "f5f5f5").
  228predefined_colour("trueGray-200", "e5e5e5").
  229predefined_colour("trueGray-300", "d4d4d4").
  230predefined_colour("trueGray-400", "a3a3a3").
  231predefined_colour("trueGray-500", "737373").
  232predefined_colour("trueGray-600", "525252").
  233predefined_colour("trueGray-700", "404040").
  234predefined_colour("trueGray-800", "262626").
  235predefined_colour("trueGray-900", "171717").
  236predefined_colour("trueGray-50", "fafafa").
  237
  238predefined_colour("gray-100", "f4f4f5").
  239predefined_colour("gray-200", "e4e4e7").
  240predefined_colour("gray-300", "d4d4d8").
  241predefined_colour("gray-400", "a1a1aa").
  242predefined_colour("gray-500", "71717a").
  243predefined_colour("gray-600", "52525b").
  244predefined_colour("gray-700", "3f3f46").
  245predefined_colour("gray-800", "27272a").
  246predefined_colour("gray-900", "18181b").
  247predefined_colour("gray-50", "fafafa").
  248
  249predefined_colour("coolGray-100", "f3f4f6").
  250predefined_colour("coolGray-200", "e5e7eb").
  251predefined_colour("coolGray-300", "d1d5db").
  252predefined_colour("coolGray-400", "9ca3af").
  253predefined_colour("coolGray-500", "6b7280").
  254predefined_colour("coolGray-600", "4b5563").
  255predefined_colour("coolGray-700", "374151").
  256predefined_colour("coolGray-800", "1f2937").
  257predefined_colour("coolGray-900", "111827").
  258predefined_colour("coolGray-50", "f9fafb").
  259
  260predefined_colour("blueGray-100", "f1f5f9").
  261predefined_colour("blueGray-200", "e2e8f0").
  262predefined_colour("blueGray-300", "cbd5e1").
  263predefined_colour("blueGray-400", "94a3b8").
  264predefined_colour("blueGray-500", "64748b").
  265predefined_colour("blueGray-600", "475569").
  266predefined_colour("blueGray-700", "334155").
  267predefined_colour("blueGray-800", "1e293b").
  268predefined_colour("blueGray-900", "0f172a").
  269predefined_colour("blueGray-50", "f8fafc").
  270
  271rgb_values([R, G, B], R, G, B).
  272rgb_values([R0, R1, G0, G1, B0, B1], R, G, B) :-
  273    R is R0 * 16 + R1,
  274    G is G0 * 16 + G1,
  275    B is B0 * 16 + B1.
  276rgba_values([R, G, B, A], R, G, B, A).
  277rgba_values([R0, R1, G0, G1, B0, B1, A0, A1], R, G, B, A) :-
  278    R is R0 * 16 + R1,
  279    G is G0 * 16 + G1,
  280    B is B0 * 16 + B1,
  281    A is A0 * 16 + A1.
 colour(-Colour)// is det
DCG for parsing a colour in one of the many formats supported by Tailwind/Girouette -- a preset list of colours, rgb, rgba, hsl, hsla.
  288colour(rgba(R, G, B, A)) -->
  289    optional("#", "rgba-"), xdigits(Digits),
  290    { length(Digits, Len),
  291      ( Len == 4 -> true ; Len == 8 ),
  292      rgba_values(Digits, R, G ,B, A) }, !.
  293colour(rgb(R, G, B)) -->
  294    optional("#", "rgb-"), xdigits(Digits),
  295    { length(Digits, Len),
  296      ( Len == 3 -> true ; Len == 6 ),
  297      rgb_values(Digits, R, G ,B) }, !.
  298
  299colour(hsla(H, S, L, A)) -->
  300    "hsla-", number(H), "-", number(S), "-", number(L), "-", number(A), !.
  301colour(hsl(H, S, L)) -->
  302    "hsl-", number(H), "-", number(S), "-", number(L), !.
  303
  304colour(special(transparent)) --> "transparent", !.
  305colour(special(currentColour)) --> "current", !.
  306
  307colour(rgba(R, G, B, A)) -->
  308    predef_colour(rgb(R, G, B)), "-", number(Opacity), !,
  309    { A is max(0, min(255, (Opacity * 255) // 100)) }.
  310colour(Colour) --> predef_colour(Colour), !.
 has_alpha(+Colour) is semidet
Predicate that succeeds when Colour has an alpha component.
  315has_alpha(rgba(_, _, _, _)).
  316has_alpha(hsla(_, _, _, _)).
 colour_with_alpha(+Colour0, +Alpha, -Colour1) is semidet
When Colour0 is a colour without an alpha channel, unifies Color1 with Colour0 but having the alpha value Alpha.
  322colour_with_alpha(rgb(R, G, B), Alpha, rgba(R, G, B, Alpha)).
  323colour_with_alpha(hsl(H, S, L), Alpha255, hsla(H, S, L, Alpha)) :-
  324    format(string(Alpha), "calc(~w / 255)", [Alpha255]).
 as_transparent(+Colour0, -Colour1) is det
True when Colour1 is Colour0 but with an alpha channel specified.
  330as_transparent(rgb(R, G, B), rgba(R, G, B, 0)) :- !.
  331as_transparent(rgba(R, G, B, _), rgba(R, G, B, 0)) :- !.
  332as_transparent(hsl(H, S, L), hsla(H, S, L, 0)) :- !.
  333as_transparent(hsla(H, S, L, _), hsla(H, S, L, 0)) :- !.
  334as_transparent(Colour, Colour).
 colour_css(+Colour, -CssStr) is det
Unifies CssStr with the CSS string representation of Colour.
  339colour_css(rgb(R, G, B), S) :-
  340    format(string(S), "#~|~`0t~16r~2+~|~`0t~16r~2+~|~`0t~16r~2+", [R, G, B]).
  341colour_css(rgba(R, G, B, A), S) :-
  342    string(A), !,
  343    format(string(S), "rgba(~w, ~w, ~w, ~w)", [R, G, B, A]).
  344colour_css(rgba(R, G, B, A), S) :-
  345    format(string(S), "#~|~`0t~16r~2+~|~`0t~16r~2+~|~`0t~16r~2+~|~`0t~16r~2+",
  346           [R, G, B, A]).
  347colour_css(hsl(H, S, L), Css) :-
  348    format(string(Css), "hsl(~w, ~w, ~w)", [H, S, L]).
  349colour_css(hsla(H, S, L, A), Css) :-
  350    format(string(Css), "hsla(~w, ~w, ~w, ~w)", [H, S, L, A])