Skip to content

Instantly share code, notes, and snippets.

@cleure
Last active August 29, 2015 14:24
Show Gist options
  • Save cleure/15245887d71552e79009 to your computer and use it in GitHub Desktop.
Save cleure/15245887d71552e79009 to your computer and use it in GitHub Desktop.
import os, sys, math
from imagekit import *
def rgb2hsv(r, g, b):
#
# https://en.wikipedia.org/wiki/HSL_and_HSV
#
h = 0.0
M = max(r, g, b)
m = min(r, g, b)
C = M - m
if C == 0:
h = 0.0
elif M == r:
h = ((g - b) / C) % 6
elif M == g:
h = (b - r) / C + 2
else:
h = (r - g) / C + 4
return [h / 6.0, C / M if M > 0 else 0.0, M]
def hsv2rgb(h, s, v):
# Initialize with 0, 0, 0
r, g, b = 0, 0, 0
C = v * s
H = h * 6
X = C * (1 - abs((H % 2) - 1))
m = v - C
if 0 <= H < 1:
r, g, b = C, X, 0
elif 1 <= H < 2:
r, g, b, = X, C, 0
elif 2 <= H < 3:
r, g, b, = 0, C, X
elif 3 <= H < 4:
r, g, b = 0, X, C
elif 4 <= H < 5:
r, g, b = X, 0, C
elif 5 <= H < 6:
r, g, b = C, 0, X
return r + m, g + m, b + m
def main():
rgb = 0.8, 0.2, 0.6
color = rgb2hsv(*rgb)
print([i * 255 for i in rgb])
print(color)
print(color[0] * 360, color[1], color[2])
b = ImageBuffer(256, 256, 3)
b.to_hsv()
for y in range(b.height):
for x in range(b.width):
b.set_pixel(x, y, (color[0] * 360, color[1], color[2]))
b.save_png('output.png')
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment