Skip to content

Instantly share code, notes, and snippets.

@zmic
Created July 15, 2021 21:08
Show Gist options
  • Save zmic/660ce74dea7900b547a49e83549fd339 to your computer and use it in GitHub Desktop.
Save zmic/660ce74dea7900b547a49e83549fd339 to your computer and use it in GitHub Desktop.
graphic4
import os
import numpy as np
from PIL import Image
#-------------------------------------------------------------------
#
# helper functions
#
def create_complex_plane(X, Y, x0, x1, y0, y1, endpoint=False):
I = np.indices((Y,X)).astype(np.float64)
fy = (y1 - y0) / (Y - 1) if endpoint else (y1 - y0) / Y
fx = (x1 - x0) / (X - 1) if endpoint else (x1 - x0) / X
return (x0 + fx*I[1]) + 1j*(y0 + fy*I[0])
def gradient(R, bins, left, right= None):
bin_widths = bins[1:] - bins[:-1]
D = np.digitize(R, bins) - 1
C0 = np.take(left, D, axis=0)
if right is None:
return C0
# create gradients between left and right
C1 = np.take(right, D, axis=0)
F = (R - np.take(bins, D))/np.take(bin_widths, D)
C = (1-F)*C0 + F*C1
return C
def normalize(x, floor = -1e+300, ceil = +1e+300, nan_replace = 0.0):
x = np.where(np.isnan(x), nan_replace, x)
x = np.maximum(x, floor)
x = np.minimum(x, ceil)
xmin, xmax = np.min(x), np.max(x)
return (x - xmin) / (xmax - xmin)
def save_hsv(H, S, V, path):
H = (H*255/(2*np.pi)).astype(np.uint8)
S = (S*255).astype(np.uint8)
V = (V*255).astype(np.uint8)
HSV = np.dstack((H, S, V))
im = Image.fromarray(HSV, mode='HSV')
im.convert('RGB').save(path)
#-------------------------------------------------------------------
stops_0 = np.array([-1.00001, -1e-05, 0.25, 0.53125, 0.625, 1.00001, 2.00001])
values_left_0 = [2.86492189803275, 4.698623254760179, 3.457225437935598, 3.7918834264604224, 2.496658308359455, 0.3395521481846612]
values_right_0 = [2.86492189803275, 6.115746101538593, 1.3543053934788272, 3.18576589688516, 2.496658308359455, 0.3395521481846612]
stops_1 = np.array([-1.00001, -1e-05, 1.00001, 2.00001])
values_left_1 = [0.9921943038795653, 0.6664993476731257, 0.6664993476731257]
values_right_1 = [0.9921943038795653, 0.6664993476731257, 0.6664993476731257]
stops_2 = np.array([-1.00001, -1e-05, 0.21875, 0.25, 0.34375, 0.375, 0.40625, 0.4375, 0.46875, 0.71875, 1.00001, 2.00001])
values_left_2 = [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]
values_right_2 = [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0]
#-------------------------------------------------------------------
W, H = 800, 800
x0, x1 = 0.03, -0.03
y0, y1 = 0.03, -0.03
r0 = create_complex_plane(W, H, x0, x1, y0, y1, endpoint=False)
r1 = ((-15.249527681959963+8.668211779907674j) + ((-0.7054031926415091-0.0047457884694788776j) * r0))
r2 = (r0 * r0)
r3 = (r2 * r2)
r4 = ((r1 + ((0.16031530695140064-0.042085564548966635j) * r2)) + ((0.0027314331527144237-0.0006840927695600108j) * r3))
r5 = (r4 + ((-0.08533582375877406-0.0061846172273119825j) * (r3 * r3)))
r6 = np.sqrt(np.arccos(np.log(np.sinh(np.tan(np.tanh(r5))))))
r7 = ((-0.0026811793904167568+1.2520350931269721j) * (np.cos(np.real(r6)) + (1j * np.sin(np.imag(r6)))))
r8 = ((-0.07463529612373146-0.3659012519588526j) + np.exp(np.real(r6)))
r9 = np.sinh(np.divide(r7, r8))
r10 = ((1.8190228888554532+2.0279394102793478j) + ((1.8997016296246896+2.0988468801639946j) * r9))
r11 = (r9 * r9)
r12 = (r11 * r11)
r13 = ((r10 + ((-0.07275161093543825-1.7603956448130778j) * r11)) + ((-0.0007946563858814499-0.0007917423801263266j) * r12))
r14 = (r13 + ((0.5404520045270538-0.08072393820287956j) * (r12 * r12)))
r15 = np.angle(r14)
r16 = normalize(r15, -1e+300, 1e+300, 0.0)
r17 = gradient(r16, stops_0, values_left_0, values_right_0)
r18 = gradient(r16, stops_1, values_left_1, values_right_1)
r19 = gradient(r16, stops_2, values_left_2, values_right_2)
#-------------------------------------------------------------------
save_hsv(r17, r18, r19, 'out.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment