|
| 1 | +{ |
| 2 | + lib, |
| 3 | + nodePackages, |
| 4 | + stdenvNoCC, |
| 5 | +}: |
| 6 | +let |
| 7 | + |
| 8 | + # inherits |
| 9 | + |
| 10 | + inherit (builtins) |
| 11 | + baseNameOf |
| 12 | + foldl' |
| 13 | + toString |
| 14 | + ; |
| 15 | + |
| 16 | + inherit (lib.attrsets) |
| 17 | + attrValues |
| 18 | + getAttr |
| 19 | + listToAttrs |
| 20 | + mapAttrs |
| 21 | + mapAttrs' |
| 22 | + nameValuePair |
| 23 | + ; |
| 24 | + |
| 25 | + inherit (lib.lists) |
| 26 | + concatMap |
| 27 | + head |
| 28 | + map |
| 29 | + toList |
| 30 | + ; |
| 31 | + |
| 32 | + inherit (lib.strings) |
| 33 | + concatStringsSep |
| 34 | + match |
| 35 | + replaceStrings |
| 36 | + toJSON |
| 37 | + toLower |
| 38 | + ; |
| 39 | + |
| 40 | + inherit (lib.trivial) |
| 41 | + flip |
| 42 | + importTOML |
| 43 | + pipe |
| 44 | + ; |
| 45 | + |
| 46 | + # reshaping |
| 47 | + |
| 48 | + /** |
| 49 | + Converts oklch values to a Tailwind string. |
| 50 | +
|
| 51 | + # Examples |
| 52 | + :::{.example} |
| 53 | + ## `oklchToTailwind` usage example |
| 54 | +
|
| 55 | + ```nix |
| 56 | + oklchToTailwind [0.55 0.11 264] |
| 57 | + => "oklch(0.550000 0.110000 264)" |
| 58 | + ``` |
| 59 | +
|
| 60 | + ::: |
| 61 | + */ |
| 62 | + oklchToTailwind = value: "oklch(${toString value})"; |
| 63 | + |
| 64 | + /** |
| 65 | + Gets the lightness value from a string. |
| 66 | +
|
| 67 | + # Examples |
| 68 | + :::{.example} |
| 69 | + ## `getLightnessValue` usage example |
| 70 | +
|
| 71 | + ```nix |
| 72 | + getLightnessValue "L95" |
| 73 | + => "95" |
| 74 | + ``` |
| 75 | +
|
| 76 | + ::: |
| 77 | + */ |
| 78 | + getLightnessValue = flip pipe [ |
| 79 | + (match "[[:alpha:]]([[:digit:]]+)") |
| 80 | + head |
| 81 | + ]; |
| 82 | + |
| 83 | + /** |
| 84 | + Convert a sequence of space-delimited words to kebab case. Will try to coerce |
| 85 | + non-string types to string. |
| 86 | +
|
| 87 | + # Type |
| 88 | +
|
| 89 | + ``` |
| 90 | + toKebab :: String | [String] -> String |
| 91 | + ``` |
| 92 | +
|
| 93 | + # Examples |
| 94 | + :::{.example} |
| 95 | + ## `toKebab` usage example |
| 96 | +
|
| 97 | + ```nix |
| 98 | + toKebab [ "1" "2" "3" ] |
| 99 | + => "1-2-3" |
| 100 | + toKebab [ 1 2 3 ] |
| 101 | + => "1-2-3" |
| 102 | + toKebab [ "1 2 3" ] |
| 103 | + => "1-2-3" |
| 104 | + ``` |
| 105 | +
|
| 106 | + ::: |
| 107 | + */ |
| 108 | + toKebab = flip pipe [ |
| 109 | + toList |
| 110 | + (map toString) |
| 111 | + (concatStringsSep " ") |
| 112 | + (replaceStrings [ " " ] [ "-" ]) |
| 113 | + toLower |
| 114 | + ]; |
| 115 | + |
| 116 | + /** |
| 117 | + Converts the tints of a color to a Tailwind CSS string. |
| 118 | +
|
| 119 | + The color optionally has an attribute `tints`. Each tint attribute |
| 120 | + contained should have a name of the form `XY+` where `X` is an alpha |
| 121 | + character and `Y` is one or more digits. The alpha character will be |
| 122 | + removed and the color values will be inserted into a string. |
| 123 | +
|
| 124 | + # Type |
| 125 | +
|
| 126 | + mapTintsToTailwind :: Attrset -> Attrset |
| 127 | +
|
| 128 | + # Examples |
| 129 | + :::{.example} |
| 130 | + ## `mapTintsToTailwind` usage example |
| 131 | +
|
| 132 | + ```nix |
| 133 | + mapTintsToTailwind { |
| 134 | + name = "Italian Violet"; |
| 135 | + tints = { |
| 136 | + L15 = [ 0.15 0.03 288 ]; |
| 137 | + L55 = [ 0.55 0.11 288 ]; |
| 138 | + L95 = [ 0.95 0.02 288 ]; |
| 139 | + }; |
| 140 | + value = [ 0.75 0.12 288 ]; |
| 141 | + } |
| 142 | + => { |
| 143 | + "15" = "oklch(0.150000 0.030000 288)"; |
| 144 | + "55" = "oklch(0.550000 0.110000 288)"; |
| 145 | + "95" = "oklch(0.950000 0.020000 288)"; |
| 146 | + } |
| 147 | +
|
| 148 | + mapTintsToTailwind { |
| 149 | + name = "NixOS Dark Blue"; |
| 150 | + value = [ 0.55 0.12 264 ]; |
| 151 | + } |
| 152 | + => { } |
| 153 | + ``` |
| 154 | +
|
| 155 | + ::: |
| 156 | + */ |
| 157 | + mapTintsToTailwind = |
| 158 | + member: |
| 159 | + mapAttrs' ( |
| 160 | + shadeName: shadeValue: nameValuePair (getLightnessValue shadeName) (oklchToTailwind shadeValue) |
| 161 | + ) member.tints or { }; |
| 162 | + |
| 163 | + /** |
| 164 | + Convert a color to a name value pair that is compatible with Tailwind CSS. |
| 165 | +
|
| 166 | + # Inputs |
| 167 | +
|
| 168 | + `groupName` |
| 169 | +
|
| 170 | + : 1\. Name of the parent color group |
| 171 | +
|
| 172 | + `member` |
| 173 | +
|
| 174 | + : 2\. The color |
| 175 | +
|
| 176 | + # Type |
| 177 | +
|
| 178 | + mapColorToTailwind :: |
| 179 | + String -> |
| 180 | + { name :: String; tints :: Maybe Attrset; value :: [Number]; } -> |
| 181 | + { name :: String; value :: { DEFAULT :: String; Maybe XX :: String}; } |
| 182 | +
|
| 183 | + # Examples |
| 184 | + :::{.example} |
| 185 | + ## `mapColorToTailwind` usage example |
| 186 | +
|
| 187 | + ```nix |
| 188 | + mapColorToTailwind "accent" { |
| 189 | + name = "Italian Violet"; |
| 190 | + tints = { |
| 191 | + L15 = [ 0.15 0.03 288 ]; |
| 192 | + L55 = [ 0.55 0.11 288 ]; |
| 193 | + L95 = [ 0.95 0.02 288 ]; |
| 194 | + }; |
| 195 | + value = [ 0.75 0.12 288 ]; |
| 196 | + } |
| 197 | + => { |
| 198 | + "name": "accent-italian-violet", |
| 199 | + "value": { |
| 200 | + "15": "oklch(0.150000 0.030000 288)", |
| 201 | + "55": "oklch(0.550000 0.110000 288)", |
| 202 | + "95": "oklch(0.950000 0.020000 288)", |
| 203 | + "DEFAULT": "oklch(0.750000 0.120000 288)" |
| 204 | + } |
| 205 | + } |
| 206 | +
|
| 207 | + mapColorToTailwind "rainbow" { |
| 208 | + "name": "red", |
| 209 | + "value": [ 0.51, 0.208963, 29.2339 ] |
| 210 | + } |
| 211 | + => { |
| 212 | + "name": "rainbow-red", |
| 213 | + "value": { |
| 214 | + "DEFAULT": "oklch(0.510000 0.208963 29.233900)" |
| 215 | + } |
| 216 | + } |
| 217 | + ``` |
| 218 | +
|
| 219 | + ::: |
| 220 | + */ |
| 221 | + mapColorToTailwind = |
| 222 | + groupName: member: |
| 223 | + nameValuePair |
| 224 | + (toKebab [ |
| 225 | + groupName |
| 226 | + member.name |
| 227 | + ]) |
| 228 | + ( |
| 229 | + { |
| 230 | + DEFAULT = oklchToTailwind member.value; |
| 231 | + } |
| 232 | + // (mapTintsToTailwind member) |
| 233 | + ); |
| 234 | + |
| 235 | + /** |
| 236 | + Convert a palette group to a structure suitable for Tailwind. |
| 237 | +
|
| 238 | + Examples |
| 239 | + :::{.example} |
| 240 | + ## `mapPaletteGroup` usage example |
| 241 | +
|
| 242 | + ```nix |
| 243 | + mapPaletteGroup { |
| 244 | + "primary": [ |
| 245 | + { |
| 246 | + "name": "Black", |
| 247 | + "tints": { |
| 248 | + "L15": [ 0.15, 0, 0 ], |
| 249 | + "L55": [ 0.55, 0, 0 ], |
| 250 | + "L95": [ 0.95, 0, 0 ] |
| 251 | + }, |
| 252 | + "value": [ 0, 0, 0 ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "name": "White", |
| 256 | + "tints": { |
| 257 | + "L15": [ 0.15, 0, 0 ], |
| 258 | + "L55": [ 0.55, 0, 0 ], |
| 259 | + "L95": [ 0.95, 0, 0 ] |
| 260 | + }, |
| 261 | + "value": [ 1, 0, 0 ] |
| 262 | + } |
| 263 | + ] |
| 264 | + } |
| 265 | + => { |
| 266 | + "primary": { |
| 267 | + "primary-black": { |
| 268 | + "15": "oklch(0.150000 0 0)", |
| 269 | + "55": "oklch(0.550000 0 0)", |
| 270 | + "95": "oklch(0.950000 0 0)", |
| 271 | + "DEFAULT": "oklch(0 0 0)" |
| 272 | + }, |
| 273 | + "primary-white": { |
| 274 | + "15": "oklch(0.150000 0 0)", |
| 275 | + "55": "oklch(0.550000 0 0)", |
| 276 | + "95": "oklch(0.950000 0 0)", |
| 277 | + "DEFAULT": "oklch(1 0 0)" |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + ``` |
| 282 | +
|
| 283 | + ::: |
| 284 | + */ |
| 285 | + mapPaletteGroup = mapAttrs ( |
| 286 | + groupName: groupValue: listToAttrs (map (mapColorToTailwind groupName) groupValue) |
| 287 | + ); |
| 288 | + |
| 289 | + /** |
| 290 | + Convert multiple palette groups to structures suitable for Tailwind. |
| 291 | + */ |
| 292 | + mapPaletteGroups = |
| 293 | + colors: |
| 294 | + concatMap ( |
| 295 | + flip pipe [ |
| 296 | + (flip getAttr colors) |
| 297 | + mapPaletteGroup |
| 298 | + attrValues |
| 299 | + ] |
| 300 | + ); |
| 301 | + |
| 302 | + /** |
| 303 | + Convert the palette to a structure suitable for Tailwind. |
| 304 | + */ |
| 305 | + mapPalette = |
| 306 | + colors: groups: |
| 307 | + let |
| 308 | + data = foldl' (acc: elem: acc // elem) { } (mapPaletteGroups colors groups); |
| 309 | + in |
| 310 | + "export default ${toJSON data}"; |
| 311 | + |
| 312 | + # sources |
| 313 | + |
| 314 | + colorsPath = ../nixos-color-palette; |
| 315 | + colorsFile = colorsPath + "/colors.toml"; |
| 316 | + colorsData = importTOML colorsFile; |
| 317 | + |
| 318 | + tailwindContent = mapPalette colorsData [ |
| 319 | + "logos" |
| 320 | + "palette" |
| 321 | + ]; |
| 322 | + |
| 323 | +in |
| 324 | + |
| 325 | +stdenvNoCC.mkDerivation { |
| 326 | + |
| 327 | + pname = baseNameOf ./.; |
| 328 | + version = colorsData.version; |
| 329 | + |
| 330 | + src = ""; |
| 331 | + |
| 332 | + runLocal = true; |
| 333 | + dontUnpack = true; |
| 334 | + dontPatch = true; |
| 335 | + dontConfigure = true; |
| 336 | + dontBuild = true; |
| 337 | + dontFixup = true; |
| 338 | + |
| 339 | + installPhase = '' |
| 340 | + mkdir $out |
| 341 | +
|
| 342 | + cat > $out/tailwind.js <<EOF |
| 343 | + ${tailwindContent} |
| 344 | + EOF |
| 345 | + ${nodePackages.prettier}/bin/prettier --write $out/tailwind.js |
| 346 | + ''; |
| 347 | + |
| 348 | +} |
0 commit comments