Created
March 15, 2013 16:18
-
-
Save endolith/5171085 to your computer and use it in GitHub Desktop.
All RGB cube wall colors on (Lightness, hue) plane
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Mar 14 21:24:01 2013 | |
All possible (integer 0-255) colors from the walls of the RGB cube (so maxed-out chroma?) sorted by lightness (0 to 100) and hue (-pi to pi), using colorpy or colormath | |
""" | |
from __future__ import division | |
from numpy import arange, empty_like, angle, concatenate | |
from colorpy.colormodels import lab_from_xyz, xyz_from_rgb, rgb_from_irgb | |
#from colormath.color_objects import RGBColor | |
from matplotlib.pyplot import figure, scatter, ylim, xlim | |
import numpy as np | |
# From http://stackoverflow.com/a/1235363/125507 | |
def cartesian(arrays, out=None): | |
""" | |
Generate a cartesian product of input arrays. | |
Parameters | |
---------- | |
arrays : list of array-like | |
1-D arrays to form the cartesian product of. | |
out : ndarray | |
Array to place the cartesian product in. | |
Returns | |
------- | |
out : ndarray | |
2-D array of shape (M, len(arrays)) containing cartesian products | |
formed of input arrays. | |
Examples | |
-------- | |
>>> cartesian(([1, 2, 3], [4, 5], [6, 7])) | |
array([[1, 4, 6], | |
[1, 4, 7], | |
[1, 5, 6], | |
[1, 5, 7], | |
[2, 4, 6], | |
[2, 4, 7], | |
[2, 5, 6], | |
[2, 5, 7], | |
[3, 4, 6], | |
[3, 4, 7], | |
[3, 5, 6], | |
[3, 5, 7]]) | |
""" | |
arrays = [np.asarray(x) for x in arrays] | |
dtype = arrays[0].dtype | |
n = np.prod([x.size for x in arrays]) | |
if out is None: | |
out = np.zeros([n, len(arrays)], dtype=dtype) | |
m = n / arrays[0].size | |
out[:,0] = np.repeat(arrays[0], m) | |
if arrays[1:]: | |
cartesian(arrays[1:], out=out[0:m,1:]) | |
for j in xrange(1, arrays[0].size): | |
out[j*m:(j+1)*m,1:] = out[0:m,1:] | |
return out | |
fig = figure(figsize=(13,9)) | |
ax = fig.add_subplot(111) | |
ax.patch.set_facecolor((0.5, 0.5, 0.5)) | |
# Random sampling of all RGB colors | |
#rgb = random_integers(0, 255, (20000,3)) | |
# 6 walls of the RGB cube only | |
span = arange(0, 256, 5) | |
rgb1 = cartesian(([0], span, span)) | |
rgb2 = cartesian(([255], span, span)) | |
rgb3 = cartesian((span, [0], span)) | |
rgb4 = cartesian((span, [255], span)) | |
rgb5 = cartesian((span, span, [0])) | |
rgb6 = cartesian((span, span, [255])) | |
rgb = concatenate((rgb1, rgb2, rgb3, rgb4, rgb5, rgb6)) | |
lab = empty_like(rgb) | |
for n, x in enumerate(rgb): | |
# colorpy | |
lab[n] = lab_from_xyz(xyz_from_rgb(rgb_from_irgb(x))) | |
# # colormath | |
# lab[n] = RGBColor(*x).convert_to('lab').get_value_tuple() | |
scatter(angle(lab[:,1]+lab[:,2]*1j), lab[:,0], color=rgb/255.0) | |
ylim(-2, 102) | |
xlim(-3.25, 3.25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment