Last active
January 12, 2019 04:10
-
-
Save fcard/1f9e1e684eb2d0614ce9b1e0e556d398 to your computer and use it in GitHub Desktop.
Pride flag/gradient generator in julia
This file contains 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
#!/usr/bin/env julia | |
module PrideFlag | |
using Cairo | |
function make_flag(n, file; line_width=1) | |
@assert n <= 256 | |
i = 0 | |
f = filtered_colors(n) | |
w = length(f) * line_width | |
c = CairoRGBSurface(round(Int, 16//9 * w), w) | |
cr = CairoContext(c) | |
for (r,g,b) in sort(f, by=hue(n)) | |
move_to(cr, 0, i*line_width+(line_width÷2)) | |
line_to(cr, c.width, i*line_width+(line_width÷2)) | |
set_line_width(cr, 1.0*line_width) | |
set_source_rgb(cr, r/n, g/n, b/n) | |
stroke(cr) | |
i += 1 | |
end | |
write_to_png(c, file) | |
end | |
hue(n) = c -> hue(c,n) | |
function hue(c,n) | |
(r,g,b) = c | |
cmax = max(r,g,b) | |
cmin = min(r,g,b) | |
h = if cmax == cmin | |
0 | |
elseif cmax == r | |
60 * (0 + (g - b)/(cmax - cmin)) | |
elseif cmax == g | |
60 * (2 + (b - r)/(cmax - cmin)) | |
else | |
60 * (4 + (r - g)/(cmax - cmin)) | |
end | |
return h < 0 ? 360 + h : h | |
end | |
function color255(c, n) | |
(x->round(Int, x)).(255//n .* c) | |
end | |
function colors_from_index(i, n) | |
r = i ÷ (n * n) | |
g = (i ÷ n) % n | |
b = i % n | |
return r,g,b | |
end | |
function colors(n) | |
[colors_from_index(i,n) for i in 0:n^3] | |
end | |
function filter_shades(shade, colors) | |
filter(x->maximum(x)==shade, colors) | |
end | |
function filter_contrast(cont, colors) | |
filter(x->maximum(x) - minimum(x) == cont, colors) | |
end | |
function filtered_colors(n) | |
filter_shades(n - 1, filter_contrast(n - 1, colors(n))) | |
end | |
module UI | |
import ..PrideFlag | |
function ask(prompt) | |
print(prompt) | |
return readline() | |
end | |
function main() | |
main(ask("possible rgb values for each color: ")) | |
end | |
function main(n) | |
main(convert_bits(n), ask("output file name: ")) | |
end | |
function main(n, file) | |
main(convert_bits(n), file, ask("line width: ")) | |
end | |
function main(ns, file, line_width_s) | |
n = convert_bits(ns) | |
line_width = convert_to_number(line_width_s) | |
PrideFlag.make_flag(n, file, line_width=line_width) | |
end | |
function convert_bits(s) | |
n = convert_to_number(s) | |
if n < 2 || n > 256 | |
error("value must be in the [2,256] range") | |
end | |
return n | |
end | |
function convert_to_number(n::Int) | |
return n | |
end | |
function convert_to_number(s::String) | |
if any(!isnumeric, s) | |
error("value must be a positive number") | |
end | |
if length(s) >= log2(typemax(Int)) | |
error("value is too large") | |
end | |
return parse(Int, s) | |
end | |
end | |
end | |
if !isinteractive() | |
PrideFlag.UI.main(ARGS...) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment