Skip to content

Instantly share code, notes, and snippets.

@skiptomyliu
Created March 11, 2017 20:51
Show Gist options
  • Save skiptomyliu/698db974f23f4ee82bd1c6d370d49c9e to your computer and use it in GitHub Desktop.
Save skiptomyliu/698db974f23f4ee82bd1c6d370d49c9e to your computer and use it in GitHub Desktop.
DEG30 = 30/360.
def adjacent_colors((r, g, b), d=DEG30): # Assumption: r, g, b in [0, 255]
r, g, b = map(lambda x: x/255., [r, g, b]) # Convert to [0, 1]
h, l, s = colorsys.rgb_to_hls(r, g, b) # RGB -> HLS
h = [(h+d) % 1 for d in (-d, d)] # Rotation by d
adjacent = [map(lambda x: int(round(x*255)), colorsys.hls_to_rgb(hi, l, s))
for hi in h] # H'LS -> new RGB
adjacent[0] = tuple(adjacent[0])
adjacent[1] = tuple(adjacent[1])
return adjacent
def hilo(a, b, c):
if c < b: b, c = c, b
if b < a: a, b = b, a
if c < b: b, c = c, b
return a + c
def complement(r, g, b):
k = hilo(r, g, b)
return tuple(k - u for u in (r, g, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment