Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,130 changes: 565 additions & 565 deletions colorpy/ColorPy.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions colorpy/blackbody.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def blackbody_spectrum (T_K):
'''Get the spectrum of a blackbody, as a numpy array.'''
spectrum = ciexyz.empty_spectrum()
(num_rows, num_cols) = spectrum.shape
for i in xrange (0, num_rows):
for i in range (0, num_rows):
specific_intensity = blackbody_specific_intensity (spectrum [i][0], T_K)
# scale by size of wavelength interval
spectrum [i][1] = specific_intensity * ciexyz.delta_wl_nm * 1.0e-9
Expand Down Expand Up @@ -135,7 +135,7 @@ def blackbody_color_vs_temperature_plot (T_list, title, filename):
'''Draw a color vs temperature plot for the given temperature range.'''
num_T = len (T_list)
rgb_list = numpy.empty ((num_T, 3))
for i in xrange (0, num_T):
for i in range (0, num_T):
T_i = T_list [i]
xyz = blackbody_color (T_i)
rgb_list [i] = colormodels.rgb_from_xyz (xyz)
Expand Down
24 changes: 12 additions & 12 deletions colorpy/ciexyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,13 @@ def init (display_intensity = DEFAULT_DISPLAY_INTENSITY):
_wavelengths [create_table_size-1] = end_wl_nm + 1
_xyz_colors [create_table_size-1] = colormodels.xyz_color (0.0, 0.0, 0.0)
# fill in the middle rows from the source data
for i in xrange (0, len (_CIEXYZ_1931_table)):
for i in range (0, len (_CIEXYZ_1931_table)):
(wl,x,y,z) = _CIEXYZ_1931_table [i]
_wavelengths [i+1] = wl
_xyz_colors [i+1] = colormodels.xyz_color (x,y,z)
# get the integrals of each curve
integral = numpy.zeros (3)
for i in xrange (0, create_table_size-1):
for i in range (0, create_table_size-1):
d_integral = 0.5 * (_xyz_colors [i] + _xyz_colors [i+1]) * delta_wl_nm
integral += d_integral
# scale the sampling curves so that:
Expand All @@ -672,7 +672,7 @@ def init (display_intensity = DEFAULT_DISPLAY_INTENSITY):
scaling = num_wl / (integral [1] * display_intensity)
_xyz_colors *= scaling
# now calculate all the deltas
for i in xrange (0, create_table_size-1):
for i in range (0, create_table_size-1):
_xyz_deltas [i] = _xyz_colors [i+1] - _xyz_colors [i]
_xyz_deltas [create_table_size-1] = colormodels.xyz_color (0.0, 0.0, 0.0)

Expand All @@ -688,10 +688,10 @@ def empty_spectrum ():

The result can be passed to xyz_from_spectrum() to convert to an xyz color.
'''
wl_nm_range = xrange (start_wl_nm, end_wl_nm + 1)
wl_nm_range = range (start_wl_nm, end_wl_nm + 1)
num_wl = len (wl_nm_range)
spectrum = numpy.zeros ((num_wl, 2))
for i in xrange (0, num_wl):
for i in range (0, num_wl):
spectrum [i][0] = float (wl_nm_range [i])
return spectrum

Expand Down Expand Up @@ -720,7 +720,7 @@ def xyz_from_spectrum (spectrum):
assert num_col == 2, 'Expecting 2D array with each row: wavelength [nm], specific intensity [W/unit solid angle]'
# integrate
rtn = colormodels.xyz_color (0.0, 0.0, 0.0)
for i in xrange (0, num_wl):
for i in range (0, num_wl):
wl_nm_i = spectrum [i][0]
specific_intensity_i = spectrum [i][1]
xyz = xyz_from_wavelength (wl_nm_i)
Expand All @@ -740,7 +740,7 @@ def get_normalized_spectral_line_colors (
dwl_angstroms - Wavelength separation, in angstroms (0.1 nm). Default 10 A. (1 nm spacing)
'''
# get range of wavelengths, in angstroms, so that we can have finer resolution than 1 nm
wl_angstrom_range = xrange (10*start_wl_nm, 10*(end_wl_nm + 1), dwl_angstroms)
wl_angstrom_range = range (10*start_wl_nm, 10*(end_wl_nm + 1), dwl_angstroms)
# get total point count
num_spectral = len (wl_angstrom_range)
num_points = num_spectral + num_purples
Expand All @@ -756,15 +756,15 @@ def get_normalized_spectral_line_colors (
# interpolate from end point to start point (filling in the purples)
first_xyz = xyzs [0]
last_xyz = xyzs [num_spectral - 1]
for ipurple in xrange (0, num_purples):
for ipurple in range (0, num_purples):
t = float (ipurple) / float (num_purples - 1)
omt = 1.0 - t
xyz = t * first_xyz + omt * last_xyz
colormodels.xyz_normalize (xyz)
xyzs [i] = xyz
i += 1
# scale each color to have the max rgb component equal to the desired brightness
for i in xrange (0, num_points):
for i in range (0, num_points):
rgb = colormodels.brightest_rgb_from_xyz (xyzs [i], brightness)
xyzs [i] = colormodels.xyz_from_rgb (rgb)
# done
Expand All @@ -784,7 +784,7 @@ def get_normalized_spectral_line_colors_annotated (
dwl_angstroms - Wavelength separation, in angstroms (0.1 nm). Default 10 A. (1 nm spacing)
'''
# get range of wavelengths, in angstroms, so that we can have finer resolution than 1 nm
wl_angstrom_range = xrange (10*start_wl_nm, 10*(end_wl_nm + 1), dwl_angstroms)
wl_angstrom_range = range (10*start_wl_nm, 10*(end_wl_nm + 1), dwl_angstroms)
# get total point count
num_spectral = len (wl_angstrom_range)
num_points = num_spectral + num_purples
Expand All @@ -803,7 +803,7 @@ def get_normalized_spectral_line_colors_annotated (
# interpolate from end point to start point (filling in the purples)
first_xyz = xyzs [0]
last_xyz = xyzs [num_spectral - 1]
for ipurple in xrange (0, num_purples):
for ipurple in range (0, num_purples):
t = float (ipurple) / float (num_purples - 1)
omt = 1.0 - t
xyz = t * first_xyz + omt * last_xyz
Expand All @@ -813,7 +813,7 @@ def get_normalized_spectral_line_colors_annotated (
names.append (name)
i += 1
# scale each color to have the max rgb component equal to the desired brightness
for i in xrange (0, num_points):
for i in range (0, num_points):
rgb = colormodels.brightest_rgb_from_xyz (xyzs [i], brightness)
xyzs [i] = colormodels.xyz_from_rgb (rgb)
# done
Expand Down
56 changes: 28 additions & 28 deletions colorpy/colormodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
simple_gamma_correct (x) -
Simple power law for gamma correction.
Not used by default.
srgb_gamma_invert (x) -

srgb_gamma_invert (x) -
sRGB standard for gamma inverse correction.
This is used by default.

Expand All @@ -159,7 +159,7 @@
phosphor_green = SRGB_Green,
phosphor_blue = SRGB_Blue,
white_point = SRGB_White) -

Setup the conversions between CIE XYZ and linear RGB spaces.
Also do other initializations (gamma, conversions with Luv and Lab spaces, clipping model).
The default arguments correspond to the sRGB standard RGB space.
Expand All @@ -175,7 +175,7 @@
display_from_linear_function = srgb_gamma_invert,
linear_from_display_function = srgb_gamma_correct,
gamma = STANDARD_GAMMA) -

Setup gamma correction.
The functions used for gamma correction/inversion can be specified,
as well as a gamma value.
Expand All @@ -187,7 +187,7 @@
into a linear component [proportional to light intensity].
The choices for the functions:
display_from_linear_function -
srgb_gamma_invert [default] - sRGB standard
srgb_gamma_invert [default] - sRGB standard
simple_gamma_invert - simple power function, can specify gamma.
linear_from_display_function -
srgb_gamma_correct [default] - sRGB standard
Expand Down Expand Up @@ -220,7 +220,7 @@
sRGB - http://www.color.org/sRGB.xalter - (accessed 15 Sep 2008)
A Standard Default Color Space for the Internet: sRGB,
Michael Stokes (Hewlett-Packard), Matthew Anderson (Microsoft), Srinivasan Chandrasekar (Microsoft),
Ricardo Motta (Hewlett-Packard), Version 1.10, November 5, 1996.
Ricardo Motta (Hewlett-Packard), Version 1.10, November 5, 1996.

License:

Expand Down Expand Up @@ -300,7 +300,7 @@ def luv_color (L, u, v):
'''Construct a Luv color from components.'''
rtn = numpy.array ([L, u, v])
return rtn

def lab_color (L, a, b):
'''Construct a Lab color from components.'''
rtn = numpy.array ([L, a, b])
Expand Down Expand Up @@ -332,7 +332,7 @@ def lab_color (L, a, b):
SMPTE_Red = xyz_color (0.630, 0.340)
SMPTE_Green = xyz_color (0.310, 0.595)
SMPTE_Blue = xyz_color (0.155, 0.070)
# use D65 as white point for SMPTE
# use D65 as white point for SMPTE

# NTSC phosphors [original standard for TV, but no longer used in TV sets]
# From Hall p. 119 and Foley/Van Dam p. 589
Expand Down Expand Up @@ -422,9 +422,9 @@ def init (
white_point = SRGB_White):
'''Setup the conversions between CIE XYZ and linear RGB spaces.
Also do other initializations (gamma, conversions with Luv and Lab spaces, clipping model).

The default arguments correspond to the sRGB standard RGB space.

The conversion is defined by supplying the chromaticities of each of
the monitor phosphors, as well as the resulting white color when all
of the phosphors are at full strength.
Expand All @@ -451,8 +451,8 @@ def init (
phosphor_blue * intensities [2]))
# invert to get rgb_from_xyz matrix
rgb_from_xyz_matrix = numpy.linalg.inv (xyz_from_rgb_matrix)
#print 'xyz_from_rgb', str (xyz_from_rgb_matrix)
#print 'rgb_from_xyz', str (rgb_from_xyz_matrix)
# print ('xyz_from_rgb %s' % str (xyz_from_rgb_matrix))
# print ('rgb_from_xyz %s' % str (rgb_from_xyz_matrix))

# conversions between the (almost) perceptually uniform
# spaces (Luv, Lab) require the definition of a white point.
Expand All @@ -467,7 +467,7 @@ def init (
def rgb_from_xyz (xyz):
'''Convert an xyz color to rgb.'''
return numpy.dot (rgb_from_xyz_matrix, xyz)

def xyz_from_rgb (rgb):
'''Convert an rgb color to xyz.'''
return numpy.dot (xyz_from_rgb_matrix, rgb)
Expand Down Expand Up @@ -669,7 +669,7 @@ def xyz_from_lab (Lab):

# Gamma correction
#
# Non-gamma corrected rgb values, also called non-linear rgb values,
# Non-gamma corrected rgb values, also called non-linear rgb values,
# correspond to palette register entries [although here they are kept
# in the range 0.0 to 1.0.] The numerical values are not proportional
# to the amount of light energy present.
Expand All @@ -691,9 +691,9 @@ def xyz_from_lab (Lab):
STANDARD_GAMMA = 2.2

# Although NTSC specifies a gamma of 2.2 as standard, this is designed
# to account for the dim viewing environments typical of TV, but not
# computers. Well-adjusted CRT displays have a true gamma in the range
# 2.35 through 2.55. We use the physical gamma value here, not 2.2,
# to account for the dim viewing environments typical of TV, but not
# computers. Well-adjusted CRT displays have a true gamma in the range
# 2.35 through 2.55. We use the physical gamma value here, not 2.2,
# thus not correcting for a dim viewing environment.
# [Poynton, Gamma FAQ p.5, p.9, Hall, p. 121]
POYNTON_GAMMA = 2.45
Expand Down Expand Up @@ -743,7 +743,7 @@ def init_gamma_correction (
'''Setup gamma correction.
The functions used for gamma correction/inversion can be specified,
as well as a gamma value.

The specified display_from_linear_function should convert a
linear (rgb) component [proportional to light intensity] into
displayable component [proportional to palette values].
Expand All @@ -754,7 +754,7 @@ def init_gamma_correction (

The choices for the functions:
display_from_linear_function -
srgb_gamma_invert [default] - sRGB standard
srgb_gamma_invert [default] - sRGB standard
simple_gamma_invert - simple power function, can specify gamma.
linear_from_display_function -
srgb_gamma_correct [default] - sRGB standard
Expand Down Expand Up @@ -826,8 +826,8 @@ def clip_rgb_color (rgb_color):
rgb [2] = scaling * (rgb [2] - rgb_min);
clipped_chromaticity = True
else:
raise ValueError, 'Invalid color clipping method %s' % (str(_clip_method))
raise ValueError ('Invalid color clipping method %s' % str(_clip_method))

# clip intensity if needed (rgb values > 1.0) by scaling
rgb_max = max (rgb)
# we actually don't overflow until 255.0 * intensity > 255.5, so instead of 1.0 use ...
Expand All @@ -839,7 +839,7 @@ def clip_rgb_color (rgb_color):
clipped_intensity = True

# gamma correction
for index in xrange (0, 3):
for index in range (0, 3):
rgb [index] = display_from_linear_component (rgb [index])

# scale to 0 - 255
Expand All @@ -851,7 +851,7 @@ def clip_rgb_color (rgb_color):
ig = min (255, max (0, ig))
ib = min (255, max (0, ib))
irgb = irgb_color (ir, ig, ib)
return (irgb, (clipped_chromaticity, clipped_intensity))
return (irgb, (clipped_chromaticity, clipped_intensity))

#
# Conversions between linear rgb colors (range 0.0 - 1.0, values proportional to light intensity)
Expand All @@ -863,7 +863,7 @@ def clip_rgb_color (rgb_color):
def irgb_string_from_irgb (irgb):
'''Convert a displayable irgb color (0-255) into a hex string.'''
# ensure that values are in the range 0-255
for index in xrange (0,3):
for index in range (0,3):
irgb [index] = min (255, max (0, irgb [index]))
# convert to hex string
irgb_string = '#%02X%02X%02X' % (irgb [0], irgb [1], irgb [2])
Expand All @@ -873,9 +873,9 @@ def irgb_from_irgb_string (irgb_string):
'''Convert a color hex string (like '#AB13D2') into a displayable irgb color.'''
strlen = len (irgb_string)
if strlen != 7:
raise ValueError, 'irgb_string_from_irgb(): Expecting 7 character string like #AB13D2'
raise ValueError ('irgb_string_from_irgb(): Expecting 7 character string like #AB13D2')
if irgb_string [0] != '#':
raise ValueError, 'irgb_string_from_irgb(): Expecting 7 character string like #AB13D2'
raise ValueError ('irgb_string_from_irgb(): Expecting 7 character string like #AB13D2')
irs = irgb_string [1:3]
igs = irgb_string [3:5]
ibs = irgb_string [5:7]
Expand Down Expand Up @@ -903,7 +903,7 @@ def rgb_from_irgb (irgb):
b = linear_from_display_component (b0)
rgb = rgb_color (r, g, b)
return rgb

def irgb_string_from_rgb (rgb):
'''Clip the rgb color, convert to a displayable color, and convert to a hex string.'''
return irgb_string_from_irgb (irgb_from_rgb (rgb))
Expand All @@ -922,6 +922,6 @@ def irgb_string_from_xyz (xyz):
# Initialization - Initialize to sRGB at module startup.
# If a different rgb model is needed, then the startup can be re-done to set the new conditions.
#

init()
# Default conversions setup on module load
15 changes: 7 additions & 8 deletions colorpy/data/massage_CIEXYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Provides a set of data sets related to color vision.
ColorPy uses the tables from this site for the 1931 CIE XYZ matching functions,
and for Illuminant D65, both at 1 nm wavelength increments.

CIE Standards - http://cvrl.ioo.ucl.ac.uk/cie.htm - (accessed 17 Sep 2008)
CIE standards as maintained by CVRL.
The 1931 CIE XYZ and D65 tables that ColorPy uses were obtained from the following files, linked here:
Expand Down Expand Up @@ -88,7 +88,7 @@ def create_CIE_XYZ_1931_table_5nm ():
keys = dict_x.keys() # all should be the same
keys.sort()
msgs.append ('_CIEXYZ_1931_table = [\n')
for i in xrange (0, len (keys)):
for i in range (0, len (keys)):
ikey = keys [i]
wl_nm = ikey
x = dict_x [ikey]
Expand All @@ -106,7 +106,7 @@ def doit_CIE_XYZ_1931_5nm ():
'''Create tables from the official CIE data.'''
msgs = create_CIE_XYZ_1931_table_5nm()
for i in msgs:
print i,
print (i),
f = open ('CIE_XYZ_1931_5nm.txt', 'w')
f.writelines (msgs)
f.close()
Expand All @@ -123,7 +123,7 @@ def create_CVRL_XYZ_1931_table_1nm ():
lines = f.readlines()
f.close()
msgs.append ('_CIEXYZ_1931_table = [\n')
for i in xrange (0, len (lines)):
for i in range (0, len (lines)):
iline = lines [i].rstrip()
sep = ','
if i == len (lines)-1:
Expand All @@ -135,7 +135,7 @@ def create_CVRL_XYZ_1931_table_1nm ():
def doit_CVRL_XYZ_1931_table_1nm ():
msgs = create_CVRL_XYZ_1931_table_1nm()
for i in msgs:
print i,
print (i),
f = open ('CVRL_XYZ_1931_1nm.txt', 'w')
f.writelines (msgs)
f.close()
Expand All @@ -149,7 +149,7 @@ def create_CVRL_D65_table_1nm ():
lines = f.readlines()
f.close()
msgs.append ('_Illuminant_D65_table = [\n')
for i in xrange (0, len (lines)):
for i in range (0, len (lines)):
iline = lines [i].rstrip()
sep = ','
if i == len (lines)-1:
Expand All @@ -161,7 +161,7 @@ def create_CVRL_D65_table_1nm ():
def doit_CVRL_D65_table_1nm ():
msgs = create_CVRL_D65_table_1nm()
for i in msgs:
print i,
print (i),
f = open ('CVRL_D65_1nm.txt', 'w')
f.writelines (msgs)
f.close()
Expand All @@ -173,4 +173,3 @@ def main ():
doit_CIE_XYZ_1931_5nm ()
doit_CVRL_XYZ_1931_table_1nm ()
doit_CVRL_D65_table_1nm ()

Loading