Skip to content

Instantly share code, notes, and snippets.

@m13253
Last active October 17, 2023 02:23
Show Gist options
  • Save m13253/78ec33af635ab7bb4e64f1c025014808 to your computer and use it in GitHub Desktop.
Save m13253/78ec33af635ab7bb4e64f1c025014808 to your computer and use it in GitHub Desktop.
Convert XYZ color values from ArgyllCMS spotread to sRGB color values
#!/usr/bin/env python3
import colour
import numpy
import readline
def convert(XYZ100):
XYZ100 = numpy.asarray(XYZ100)
XYZ1 = XYZ100 * 0.01
D50 = colour.CCS_ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['ICC D50']
sRGB1 = colour.XYZ_to_sRGB(XYZ1, illuminant=D50, chromatic_adaptation_transform='Bradford')
sRGB255 = numpy.round(sRGB1 * 255)
if numpy.all(sRGB255 >= 0) and numpy.all(sRGB255 <= 255):
R, G, B = map(int, sRGB255.tolist())
return f'#{R:02X}{G:02X}{B:02X}'
else:
sRGB255 = sRGB1 * 255
R, G, B = sRGB255.tolist()
return f'RGB({R:.1f}, {G:.1f}, {B:.1f})'
def main():
print('Please type in the CIEXYZ (D50) result of spotread.')
print('For example: 17.355600 18.000000 14.848200')
while True:
response = input('XYZ> ')
if not response:
break
XYZ100 = response.split()
X100, Y100, Z100 = map(float, XYZ100)
print('sRGB:', convert([X100, Y100, Z100]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment