Skip to content

Instantly share code, notes, and snippets.

@cormullion
Created October 9, 2020 07:51
Show Gist options
  • Save cormullion/1c37f6a7e9027493cbeba22fe9acd803 to your computer and use it in GitHub Desktop.
Save cormullion/1c37f6a7e9027493cbeba22fe9acd803 to your computer and use it in GitHub Desktop.
noisy landscape
using Luxor, Colors, ColorSchemes
"""
draw a box with noisy top edge and straight sides
"""
function layer(leftminheight, rightminheight, noisefrequency;
detail=1,
persistence=0)
bottomleft, topleft, topright, bottomright = box(BoundingBox(), vertices=true)
ip1 = between(bottomleft, topleft, leftminheight)
ip2 = between(bottomright, topright, rightminheight)
topedge = Point[]
initnoise(rand(1:100))
for x in ip1.x:2:ip2.x
ypos = between(ip1, ip2, rescale(x, ip1.x, ip2.x, 0, 1)).y
ypos *= noise(x/noisefrequency, detail=detail, persistence=persistence)
push!(topedge, Point(x, ypos))
end
poly([bottomleft, topedge..., bottomright], :fill, close=true)
end
"""
draw some noisy clouds in grey
"""
function clouds()
tiles = Tiler(boxwidth(BoundingBox()), boxheight(BoundingBox()), 800, 300, margin=0)
@layer begin
setopacity(0.15)
freq = 0.001
for (pos, n) in tiles
nv = noise(pos.x * freq, pos.y * freq, detail=4, persistence=.4)
setgray(nv)
box(pos, tiles.tilewidth, tiles.tileheight, :fill)
end
end
end
"""
create a simple intermediate RGBA color of two colors
"""
function colorblend(fromcolor, tocolor, n=0.5)
f = clamp(n, 0, 1)
nc1 = convert(RGBA, fromcolor)
nc2 = convert(RGBA, tocolor)
fromcolor_red, fromcolor_green, fromcolor_blue, fromcolor_alpha = convert.(Float64, (nc1.r, nc1.g, nc1.b, nc1.alpha))
tocolor_red, tocolor_green, tocolor_blue, tocolor_alpha = convert.(Float64, (nc2.r, nc2.g, nc2.b, nc1.alpha))
newred = (f * (tocolor_red - fromcolor_red)) + fromcolor_red
newgreen = (f * (tocolor_green - fromcolor_green)) + fromcolor_green
newblue = (f * (tocolor_blue - fromcolor_blue)) + fromcolor_blue
newalpha = (f * (tocolor_alpha - fromcolor_alpha)) + fromcolor_alpha
return RGBA(newred, newgreen, newblue, newalpha)
end
function main(scheme, schemename)
Drawing(8000, 3000, "/tmp/landscape-$(schemename)-$(rand(100:999)).png")
origin()
# sky is gradient mesh
mesh1 = mesh(box(BoundingBox(), vertices=true), [
get(scheme, rand()),
get(scheme, rand()),
get(scheme, rand()),
get(scheme, rand())
])
setmesh(mesh1)
box(BoundingBox(), :fill)
# clouds are 2D noise
clouds()
# the sun is a disk placed at random
@layer begin
setopacity(0.25)
sethue(get(scheme, .95))
sunposition = boxtop(BoundingBox()) + (rand(-boxwidth(BoundingBox()/2):boxwidth(BoundingBox()/2)), boxheight(BoundingBox())/10)
circle(sunposition, boxdiagonal(BoundingBox()/30), :fill)
end
setopacity(0.9)
# how many layers
len = 8
noiselevels = range(5000, length=len, stop=800)
detaillevels = 1:len
persistencelevels = range(0.5, length=len, stop=0.75 )
for (n, i) in enumerate(range(1, length=len, stop=0))
# avoid extremes of range
sethue(colorblend(get(scheme, .1), get(scheme, .9), i))
layer(i - rand()/3, i - rand()/3, noiselevels[n], detail=detaillevels[n], persistence=persistencelevels[n])
end
finish()
preview()
end
for (k, v) in colorschemes
main(v, k)
end
@cormullion
Copy link
Author

Screenshot 2020-10-09 at 08 53 18

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