nixos/users/malte/colors.nix
Malte Tammena 9bada411db Move [marie|malte] -> /users/[marie|malte]
To prepare for an addiotional /users/modules
which can be shared between users.
2022-01-13 12:08:39 +01:00

127 lines
3.9 KiB
Nix

{ pkgs, lib, config, ... }:
let
# Create a color from a hexadezimal 6 digit description, like 'FF0000' for pure red
# mkcolor :: hex -> Color
mkColor = rgb:
let
# Mapping hex digits to actual numbers
hexDigitMap = {
"0" = 0;
"1" = 1;
"2" = 2;
"3" = 3;
"4" = 4;
"5" = 5;
"6" = 6;
"7" = 7;
"8" = 8;
"9" = 9;
"A" = 10;
"B" = 11;
"C" = 12;
"D" = 13;
"E" = 14;
"F" = 15;
};
# Convert a single hexadezimal character to a number
# hexCharToInt :: char -> int
hexCharToInt = char: hexDigitMap.${lib.strings.toUpper char};
# Convert a hexadezimal string to a number
# hexStringToInt :: string -> int
hexStringToInt = string:
let
chars = lib.strings.stringToCharacters string;
nums = map hexCharToInt chars;
fun = sum: el: sum * 16 + el;
in lib.foldl fun 0 nums;
# Convert a float from [0.0, 1.0] to a hex string from [0, 255]
# floatToFF :: float -> string
floatToFF = float: lib.toHexString (builtins.floor (255 * float));
# Red part of the given color
# red :: int
red = hexStringToInt (lib.strings.substring 0 2 rgb);
# Green part of the given color
# green :: int
green = hexStringToInt (lib.strings.substring 2 2 rgb);
# Blue part of the given color
# blue :: int
blue = hexStringToInt (lib.strings.substring 4 2 rgb);
# Create an RGB hex string from red, green, and blue parts
# rgbFromParts :: int -> int -> int -> string
rgbFromParts = r: g: b:
let
rHex = lib.strings.fixedWidthString 2 "0" (lib.toHexString r);
gHex = lib.strings.fixedWidthString 2 "0" (lib.toHexString g);
bHex = lib.strings.fixedWidthString 2 "0" (lib.toHexString b);
in rHex + gHex + bHex;
# Create a Color darker than the color given by the rgb parts.
# amount is in range [0.0, 1.0], r,g, and b in range [0, 255]
# darkenColor :: int -> int -> int -> float -> Color
darkenColor = r: g: b: amount:
let
r' = builtins.floor ((1.0 - amount) * r);
g' = builtins.floor ((1.0 - amount) * g);
b' = builtins.floor ((1.0 - amount) * b);
in mkColor (rgbFromParts r' g' b');
# Create a Color lighter than the color given by the rgb parts. See darkenColor.
# darkenColor :: int -> int -> int -> float -> Color
lightenColor = r: g: b: amount:
let
r' = lib.min (builtins.floor ((1.0 + amount) * r)) 255;
g' = lib.min (builtins.floor ((1.0 + amount) * g)) 255;
b' = lib.min (builtins.floor ((1.0 + amount) * b)) 255;
in mkColor (rgbFromParts r' g' b');
in {
inherit rgb red green blue;
# e.g. "#FF0000"
hashRgb = "#" + rgb;
# e.g. rgbWithAlpha 1.0 -> "FF0000FF"
# rgbWithAlpha :: float -> string
rgbWithAlpha = alpha: rgb + (floatToFF alpha);
# e.g. hashRgbWithAlpha 1.0 -> "#FF0000FF"
# rgbWithAlpha :: float -> string
hashRgbWithAlpha = alpha: "#" + rgb + (floatToFF alpha);
# darker :: float -> Color
darker = darkenColor red green blue;
# lighter :: float -> Color
lighter = lightenColor red green blue;
};
in {
options.colorDef = lib.mkOption {
default = { };
type = with lib.types; attrsOf (uniq (strMatching "[0-9A-F]{6}"));
};
options.colors = lib.mkOption {
default = { };
type = with lib.types; attrsOf (uniq anything);
};
config.colors =
builtins.mapAttrs (name: hexRGB: mkColor hexRGB) config.colorDef;
config.colorDef = {
background = "282828";
foreground = "EBDBB2";
backgroundDimmed = "3C3836";
foregroundDimmed = "D5C4A1";
primary = "689D6A";
secondary = "458588";
warn = "FE8019";
error = "CC241D";
yellow = "D79921";
};
}