Created
January 18, 2018 07:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" radian = degrees * Math.PI / 180; | |
" degrees = radian * 180 / Math.PI; | |
function! s:rad_to_deg(r) abort | |
return a:r * 180.0 / 3.141592 | |
endfunction | |
function! s:deg_to_rad(d) abort | |
return a:d * 3.141592 / 180.0 | |
endfunction | |
" https://github.com/Qix-/color-convert/blob/427cbb70540bb9e5b3e94aa3bb9f97957ee5fbc0/conversions.js#L555-L580 | |
function! s:to_ansi256(rgb) abort | |
let [r, g, b] = a:rgb | |
if (r == g) && (g == b) | |
if (r < 8) | |
return 16; | |
endif | |
if (248 < r) | |
return 231 | |
endif | |
return float2nr(round(((r - 8.0) / 247) * 24) + 232) | |
endif | |
" 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) | |
" https://stackoverflow.com/a/39277954 | |
let ansi256 = 16 + | |
\ 36 * (round(r / 255.0 * 5)) + | |
\ 6 * (round(g / 255.0 * 5)) + | |
\ round(b / 255.0 * 5) | |
return float2nr(ansi256) | |
endfunction | |
" https://hail2u.net/blog/software/convert-hex-color-to-functional-color-with-vim.html | |
function! s:parse_rgb(rgb) abort | |
let [r, g, b] = matchlist(a:rgb, '\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)')[1:3] | |
return [str2nr(r, 16), str2nr(g, 16), str2nr(b, 16)] | |
endfunction | |
function! s:blend_color(base_color, mix_color, fraction) abort | |
let [r, g, b] = s:parse_rgb(a:base_color) | |
let [rr, gg, bb] = s:parse_rgb(a:mix_color) | |
let x = a:fraction | |
let y = 1 - x | |
let red = float2nr((r * y) + (rr * x)) | |
let green = float2nr((g * y) + (gg * x)) | |
let blue = float2nr((b * y) + (bb * x)) | |
return printf('#%02x%02x%02x', red, green, blue) | |
endfunction | |
function! s:make_gradient(color_stops, time) abort | |
let [sec, min, hour] = split(strftime('%S,%M,%H', a:time), ',') | |
let x0 = sec / 60.0 | |
let x = (x0 + min) / 60.0 + hour | |
let last_color = a:color_stops[0] | |
for next_color in a:color_stops | |
if x <= next_color[0] | |
let fraction = ((x - last_color[0]) / (next_color[0] - last_color[0])) | |
let c = s:blend_color(last_color[1], next_color[1], fraction) | |
echomsg 'color: ' . c | |
return c | |
endif | |
let last_color = next_color | |
endfor | |
return a:color_stops[-1][1] | |
endfunction | |
function! SkyColorClock() abort | |
let now = localtime() | |
let current_year = str2nr(strftime('%Y', localtime())) | |
let leap_count = count(map(range(1970, current_year - 1), 'v:val % 400 == 0 || (v:val % 4 == 0 && v:val % 100 != 0)'), 1) | |
let day_of_year = float2nr(localtime() / (24 * 60 * 60)) % 365 - leap_count | |
let latitude = 35 | |
let sun_declination = s:deg_to_rad(-23.44 * (cos(s:deg_to_rad((360 / 365.0) * (day_of_year + 10))))) | |
let sunset_hour_angle = acos(-1 * tan(s:deg_to_rad(latitude) * tan(sun_declination))) | |
let sunset_time_from_noon = 24 * (s:rad_to_deg(sunset_hour_angle) / 360) | |
let sunrise = 12 - sunset_time_from_noon | |
let sunset = 12 + sunset_time_from_noon | |
let colors = [ | |
\ [sunrise - 2.0, "#111111"], | |
\ [sunrise - 1.5, "#4d548a"], | |
\ [sunrise - 1.0, "#c486b1"], | |
\ [sunrise - 0.5, "#ee88a0"], | |
\ [sunrise, "#ff7d75"], | |
\ [sunrise + 0.5, "#f4eeef"], | |
\ [(sunset + sunrise) / 2, "#5dc9f1"], | |
\ [sunset - 1.5, "#9eefe0"], | |
\ [sunset - 1.0, "#f1e17c"], | |
\ [sunset - 0.5, "#f86b10"], | |
\ [sunset, "#100028"], | |
\ [sunset + 0.5, "#111111"], | |
\ ] | |
" let now = 1516201200 + 5 * 60 * 60 + 30 * 60 " 5:30 | |
" let now = 1516201200 + 5 * 60 * 60 + 45 * 60 " 5:45 | |
" let now = 1516201200 + 6 * 60 * 60 " 6:00 | |
" let now = 1516201200 + 7 * 60 * 60 + 30 * 60 " 7:30 | |
" let now = 1516201200 + 16 * 60 * 60 + 30 * 60 " 16:30 | |
execute 'hi SkyColor ctermfg=0 ctermbg=' . s:to_ansi256(s:parse_rgb(s:make_gradient(colors, now))) | |
return strftime('%H:%M:%S', now) | |
endfunction | |
" | |
set statusline=%#SkyColor#%{SkyColorClock()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment