Skip to content

Instantly share code, notes, and snippets.

@wuyoli
Last active November 23, 2022 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuyoli/3491e0fc64c3f0296785c1bd71fb6291 to your computer and use it in GitHub Desktop.
Save wuyoli/3491e0fc64c3f0296785c1bd71fb6291 to your computer and use it in GitHub Desktop.
This is a implementation of a [Base 16 Builder](https://github.com/chriskempson/base16/blob/main/builder.md) implemented in nix. First mentioned [here](https://github.com/nix-community/home-manager/issues/1005).
# author: wuyoli
# License: MIT
pkgs: template: theme:
with builtins;
with pkgs.lib;
let
fromHex = hexStr:
let
upperHexStr = toUpper hexStr;
in
if stringLength upperHexStr == 0 then
throw "fromHex can't handle empty strings"
else if stringLength upperHexStr > 1000 then
throw "fromHex can't handle strings longer than 1000 elements"
else if stringLength upperHexStr == 1 then
if match "[[:digit:]]" upperHexStr != null then
strings.toInt upperHexStr
else
if upperHexStr == "A" then
10
else if upperHexStr == "B" then
11
else if upperHexStr == "C" then
12
else if upperHexStr == "D" then
13
else if upperHexStr == "E" then
14
else if upperHexStr == "F" then
15
else
throw "string \"${hexStr}\" does not contain a hex value"
else
16 * (fromHex (substring 0 1 upperHexStr)) + (fromHex (substring 1 1001 upperHexStr));
getColor = argList:
# $1: hex code of
let
colorNr = head argList;
encoding = head (tail argList);
parameter = strings.toLower (head (tail (tail argList)));
hexRGB = theme.colors."base0${colorNr}";
hex = {
r = substring 0 2 hexRGB;
g = substring 2 2 hexRGB;
b = substring 4 2 hexRGB;
};
in
if encoding == "hex-bgr" then
hex.b ++ hex.g ++ hex.r
else if encoding == "hex" then
if parameter != null then
"hex${parameter}"
else
templateColor
else if encoding == "rgb" then
toString (fromHex hex."${parameter}")
else
throw "Something went wrong while converting the colors to the proper format.";
getValue = str:
if str == "scheme-name" then
theme.meta.name
else if str == "scheme-author" then
theme.meta.author
else if str == "scheme-slug" then
builtins.replaceStrings [ " " ] [ "-" ] (builtins.toLower theme.meta.author)
else if substring 0 1 str == "!" then # comments
""
else
let
captured = builtins.match "base0([0-F])-(hex-bgr|hex|rgb|dec|hsl)-?([rgbhsl]?)" str;
in
if captured != null then
getColor captured
else
throw "theme template contains illegal variable: ${str}";
splitList = split "\\{\\{([^}]+)}}" template;
replacedList = map (elem: if isString elem then elem else getValue (head elem)) splitList;
in
strings.concatStrings replacedList
{
meta = {
name = "OneDark";
author = "Lalit Magant (http://github.com/tilal6991)";
};
colors = {
base00 = "282c34";
base01 = "353b45";
base02 = "3e4451";
base03 = "545862";
base04 = "565c64";
base05 = "abb2bf";
base06 = "b6bdca";
base07 = "c8ccd4";
base08 = "e06c75";
base09 = "d19a66";
base0A = "e5c07b";
base0B = "98c379";
base0C = "56b6c2";
base0D = "61afef";
base0E = "c678dd";
base0F = "be5046";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment