Skip to content

Instantly share code, notes, and snippets.

@harrygallagher4
Last active April 17, 2021 05:40
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 harrygallagher4/76434f43fcc2cf3c085c6e8dae0195c4 to your computer and use it in GitHub Desktop.
Save harrygallagher4/76434f43fcc2cf3c085c6e8dae0195c4 to your computer and use it in GitHub Desktop.
lush.nvim + hsluv utils

shit this file should be at the top (edit: it is!)

here's an example of blending with the background in order to fade, I think it looks pretty consistent and nice

fade example

gradient example

gradient example

-- harryg/colorutils.lua
--
-- this was my original implementation of these utils in lua
-- but it doesn't have some of the functions from the fennel version
--
local lush = require'lush'
local hsluv = require'hsluv'
local deg, rad = math.deg, math.rad
local function linearTween(start, stop)
return function (i)
return (((stop - start) * i) + start)
end
end
local function radialTween(start, stop)
start = rad(start)
stop = rad(stop)
local delta = math.atan2(math.sin(stop - start), math.cos(stop - start))
return function(i)
return ((deg(start + (delta * i)) + 360) % 360)
end
end
local function blend_hsluv(start, stop, ratio)
ratio = ratio or 0.5
local h = radialTween(start[1], stop[1])
local s = linearTween(start[2], stop[2])
local l = linearTween(start[3], stop[3])
return {h(ratio), s(ratio), l(ratio)}
end
local function blend(start, stop, ratio)
local c1 = hsluv.hex_to_hsluv(start.hex)
local c2 = hsluv.hex_to_hsluv(stop.hex)
local mix = blend_hsluv(c1, c2, ratio)
return lush.hsl(hsluv.hsluv_to_hex(mix))
end
local function blend_hex(start, stop, ratio)
local c1 = hsluv.hex_to_hsluv(start)
local c2 = hsluv.hex_to_hsluv(stop)
local mix = blend_hsluv(c1, c2, ratio)
return hsluv.hsluv_to_hex(mix)
end
local function fader(base)
return function(color, ratio)
return blend(color, base, ratio)
end
end
return {
blend_hsluv = blend_hsluv,
blend_hex = blend_hex,
blend = blend,
fader = fader
}
;; haryg/colorutilsfnl.fnl
;;
;; i added the .clj extension in the gist because github doesn't
;; highlight fennel and the clojure highlighting works well enough
;;
;; `map` and `concat` are macro shorthands for `aniseed.core.[map|concat]`
;;
;; `dbg!` is a macro that only executes when editing this file and should
;; be safe to replace with `comment`
(module harryg.colorutilsfnl
{require {a aniseed.core
str aniseed.string
nvim aniseed.nvim
lush lush
hsluv hsluv
u harryg.colorutils}
require-macros [dotfiles.macros]})
(def- deg math.deg)
(def- rad math.rad)
(defn- linear-tween [start stop]
(fn [i]
(+ start (* i (- stop start)))))
(defn- radial-tween [x y]
(let [start (rad x)
stop (rad y)
delta (math.atan2 (math.sin (- stop start)) (math.cos (- stop start)))]
(fn [i]
(% (+ 360 (deg (+ start (* delta i)))) 360))))
; ((radial-tween 90 270) 0.5) ;; => 180
; ((linear-tween 20 80) 0.5) ;; => 50
(defn blend-hsluv [start stop ratio]
(let [ratio (or ratio 0.5)
h (radial-tween (. start 1) (. stop 1))
s (linear-tween (. start 2) (. stop 2))
l (linear-tween (. start 3) (. stop 3))]
[(h ratio) (s ratio) (l ratio)]))
(defn- blend-old [start stop ratio]
(let [c1 (hsluv.hex_to_hsluv start.hex)
c2 (hsluv.hex_to_hsluv stop.hex)]
(lush.hsl (hsluv.hsluv_to_hex (blend-hsluv c1 c2 ratio)))))
(defn blend-lush [c1 c2 r]
(-> (blend-hsluv (hsluv.hex_to_hsluv c1.hex) (hsluv.hex_to_hsluv c2.hex) r)
(hsluv.hsluv_to_hex)
(lush.hsl)))
(defn blend-hex [c1 c2 r]
(-> (blend-hsluv (hsluv.hex_to_hsluv c1) (hsluv.hex_to_hsluv c2) r)
(hsluv.hsluv_to_hex)))
(def blend blend-lush)
(dbg!
(. (blend (lush.hsl "#ff7e7e") (lush.hsl "#4a4a4a")) :hex)
(. (u.blend (lush.hsl "#ff7e7e") (lush.hsl "#4a4a4a")) :hex)
(blend-hex "#ff7e7e" "#4a4a4a")
(u.blend_hex "#ff7e7e" "#4a4a4a")
(hsluv.hsluv_to_hex (blend-hsluv (hsluv.hex_to_hsluv "#ff7e7e") (hsluv.hex_to_hsluv "#4a4a4a")))
(hsluv.hsluv_to_hex (u.blend_hsluv (hsluv.hex_to_hsluv "#ff7e7e") (hsluv.hex_to_hsluv "#4a4a4a"))))
(defn- gradient [c1 c2]
(var ls [])
(for [i 0.00 1.01 0.02]
(set ls (concat ls [i])))
(map #(blend-hex c1 c2 $1) ls))
(defn- gradient-n [c1 c2 n]
(var ls [])
(let [step (/ 1 (+ n 1))]
(for [i 1 n 1]
(set ls (concat ls [(* i step)]))))
(concat [c1] (map #(blend-hex c1 c2 $1) ls) [c2]))
; sample_cyan_xxxxxxxx xxx guifg=#85ADAD guibg=#85ADAD
; sample_teal_xxxxxxxx xxx guifg=#4DB380 guibg=#4DB380
; (gradient-n "#4DB380" "#85ADAD" 37)
(defn- palette [c]
(. (. (require :melange.colors) :Melange :lush c) :hex))
(dbg!
(palette :red)
(. (require "melange.colors") :Melange :lush)
(gradient-n (palette :green) "#2a2621" 37)
(gradient "#d65c71" "#2a2621"))
-- harryg/colorutilsfnl.lua
--
-- this is the compiled version of `colorutilsfnl.fnl`
-- sadly, it still depends on aniseed but it should be reasonably easy
-- to copy/paste some of the compiled functions to use in lua
--
local _0_0 = nil
do
local name_0_ = "harryg.colorutilsfnl"
local module_0_ = nil
do
local x_0_ = package.loaded[name_0_]
if ("table" == type(x_0_)) then
module_0_ = x_0_
else
module_0_ = {}
end
end
module_0_["aniseed/module"] = name_0_
module_0_["aniseed/locals"] = ((module_0_)["aniseed/locals"] or {})
module_0_["aniseed/local-fns"] = ((module_0_)["aniseed/local-fns"] or {})
package.loaded[name_0_] = module_0_
_0_0 = module_0_
end
local function _1_(...)
local ok_3f_0_, val_0_ = nil, nil
local function _1_()
return {require("aniseed.core"), require("hsluv"), require("lush"), require("aniseed.nvim"), require("aniseed.string"), require("harryg.colorutils")}
end
ok_3f_0_, val_0_ = pcall(_1_)
if ok_3f_0_ then
_0_0["aniseed/local-fns"] = {["require-macros"] = {["dotfiles.macros"] = true}, require = {a = "aniseed.core", hsluv = "hsluv", lush = "lush", nvim = "aniseed.nvim", str = "aniseed.string", u = "harryg.colorutils"}}
return val_0_
else
return print(val_0_)
end
end
local _local_0_ = _1_(...)
local a = _local_0_[1]
local hsluv = _local_0_[2]
local lush = _local_0_[3]
local nvim = _local_0_[4]
local str = _local_0_[5]
local u = _local_0_[6]
local _2amodule_2a = _0_0
local _2amodule_name_2a = "harryg.colorutilsfnl"
do local _ = ({nil, _0_0, {{nil}, nil, nil, nil}})[2] end
local deg = nil
do
local v_0_ = math.deg
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["deg"] = v_0_
deg = v_0_
end
local rad = nil
do
local v_0_ = math.rad
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["rad"] = v_0_
rad = v_0_
end
local linear_tween = nil
do
local v_0_ = nil
local function linear_tween0(start, stop)
local function _2_(i)
return (start + (i * (stop - start)))
end
return _2_
end
v_0_ = linear_tween0
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["linear-tween"] = v_0_
linear_tween = v_0_
end
local radial_tween = nil
do
local v_0_ = nil
local function radial_tween0(x, y)
local start = rad(x)
local stop = rad(y)
local delta = math.atan2(math.sin((stop - start)), math.cos((stop - start)))
local function _2_(i)
return ((360 + deg((start + (delta * i)))) % 360)
end
return _2_
end
v_0_ = radial_tween0
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["radial-tween"] = v_0_
radial_tween = v_0_
end
local blend_hsluv = nil
do
local v_0_ = nil
do
local v_0_0 = nil
local function blend_hsluv0(start, stop, ratio)
local ratio0 = (ratio or 0.5)
local h = radial_tween(start[1], stop[1])
local s = linear_tween(start[2], stop[2])
local l = linear_tween(start[3], stop[3])
return {h(ratio0), s(ratio0), l(ratio0)}
end
v_0_0 = blend_hsluv0
_0_0["blend-hsluv"] = v_0_0
v_0_ = v_0_0
end
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["blend-hsluv"] = v_0_
blend_hsluv = v_0_
end
local blend_old = nil
do
local v_0_ = nil
local function blend_old0(start, stop, ratio)
local c1 = hsluv.hex_to_hsluv(start.hex)
local c2 = hsluv.hex_to_hsluv(stop.hex)
return lush.hsl(hsluv.hsluv_to_hex(blend_hsluv(c1, c2, ratio)))
end
v_0_ = blend_old0
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["blend-old"] = v_0_
blend_old = v_0_
end
local blend_lush = nil
do
local v_0_ = nil
do
local v_0_0 = nil
local function blend_lush0(c1, c2, r)
return lush.hsl(hsluv.hsluv_to_hex(blend_hsluv(hsluv.hex_to_hsluv(c1.hex), hsluv.hex_to_hsluv(c2.hex), r)))
end
v_0_0 = blend_lush0
_0_0["blend-lush"] = v_0_0
v_0_ = v_0_0
end
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["blend-lush"] = v_0_
blend_lush = v_0_
end
local blend_hex = nil
do
local v_0_ = nil
do
local v_0_0 = nil
local function blend_hex0(c1, c2, r)
return hsluv.hsluv_to_hex(blend_hsluv(hsluv.hex_to_hsluv(c1), hsluv.hex_to_hsluv(c2), r))
end
v_0_0 = blend_hex0
_0_0["blend-hex"] = v_0_0
v_0_ = v_0_0
end
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["blend-hex"] = v_0_
blend_hex = v_0_
end
local blend = nil
do
local v_0_ = nil
do
local v_0_0 = blend_lush
_0_0["blend"] = v_0_0
v_0_ = v_0_0
end
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["blend"] = v_0_
blend = v_0_
end
do
local a_0_ = require("aniseed.core")
local s_0_ = require("aniseed.string")
local v_0_ = require("aniseed.nvim")
if ((a_0_.last(s_0_.split(_2amodule_name_2a, "%.")) .. ".fnl") == a_0_.last(s_0_.split(v_0_.buf_get_name(0), "/"))) then
do local _ = (blend(lush.hsl("#ff7e7e"), lush.hsl("#4a4a4a"))).hex end
do local _ = (u.blend(lush.hsl("#ff7e7e"), lush.hsl("#4a4a4a"))).hex end
blend_hex("#ff7e7e", "#4a4a4a")
u.blend_hex("#ff7e7e", "#4a4a4a")
hsluv.hsluv_to_hex(blend_hsluv(hsluv.hex_to_hsluv("#ff7e7e"), hsluv.hex_to_hsluv("#4a4a4a")))
hsluv.hsluv_to_hex(u.blend_hsluv(hsluv.hex_to_hsluv("#ff7e7e"), hsluv.hex_to_hsluv("#4a4a4a")))
end
end
local gradient = nil
do
local v_0_ = nil
local function gradient0(c1, c2)
local ls = {}
for i = 0, 1.01, 0.02 do
ls = a.concat(ls, {i})
end
local function _2_(_241)
return blend_hex(c1, c2, _241)
end
return a.map(_2_, ls)
end
v_0_ = gradient0
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["gradient"] = v_0_
gradient = v_0_
end
local gradient_n = nil
do
local v_0_ = nil
local function gradient_n0(c1, c2, n)
local ls = {}
do
local step = (1 / (n + 1))
for i = 1, n, 1 do
ls = a.concat(ls, {(i * step)})
end
end
local function _2_(_241)
return blend_hex(c1, c2, _241)
end
return a.concat({c1}, a.map(_2_, ls), {c2})
end
v_0_ = gradient_n0
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["gradient-n"] = v_0_
gradient_n = v_0_
end
local palette = nil
do
local v_0_ = nil
local function palette0(c)
return ((require("melange.colors")).Melange.lush[c]).hex
end
v_0_ = palette0
local t_0_ = (_0_0)["aniseed/locals"]
t_0_["palette"] = v_0_
palette = v_0_
end
local a_0_ = require("aniseed.core")
local s_0_ = require("aniseed.string")
local v_0_ = require("aniseed.nvim")
if ((a_0_.last(s_0_.split(_2amodule_name_2a, "%.")) .. ".fnl") == a_0_.last(s_0_.split(v_0_.buf_get_name(0), "/"))) then
return palette("red"), (require("melange.colors")).Melange.lush, gradient_n(palette("green"), "#2a2621", 37), gradient("#d65c71", "#2a2621")
end
@harrygallagher4
Copy link
Author

harrygallagher4 commented Apr 16, 2021

images

image

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment