Skip to content

Instantly share code, notes, and snippets.

@curioustorvald
Last active October 31, 2022 07:13
Show Gist options
  • Save curioustorvald/4c04ab9def2cf8d9fdec2890f5ee3c15 to your computer and use it in GitHub Desktop.
Save curioustorvald/4c04ab9def2cf8d9fdec2890f5ee3c15 to your computer and use it in GitHub Desktop.
# usage: python3 cull.py HUE
# where HUE is a number
#
# Get datasets faster using multiple threads, like:
# seq 360 | xargs --max-procs=8 -n 1 python cull.py
# Copyright © 2022 CuriousTorvald
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import colour # pip install colour-science
import math
import sys
def lch_to_lab(l,c,h):
hrad = h * math.pi / 180.0
a = c * math.cos(hrad)
b = c * math.sin(hrad)
return [l, a, b]
def lab_to_xyz(L,a,b):
if (L < 0.000001):
return [0.0, 0.0, 0.0]
#FIXME D65 only!
l = math.pow(0.99999999845051981432*L + 0.39633779217376785678*a + 0.21580375806075880339*b, 3.0)
m = math.pow(1.0000000088817607767*L - 0.1055613423236563494*a - 0.063854174771705903402*b, 3.0)
s = math.pow(1.0000000546724109177*L - 0.089484182094965759684*a - 1.2914855378640917399*b, 3.0)
X = 1.227013851103521026*l - 0.5577999806518222383*m + 0.28125614896646780758*s
Y = -0.040580178423280593977*l + 1.1122568696168301049*m - 0.071676678665601200577*s
Z = -0.076381284505706892869*l - 0.42148197841801273055*m + 1.5861632204407947575*s
return [X, Y, Z]
def lch_to_xyz(l,c,h):
lab = lch_to_lab(l,c,h)
return lab_to_xyz(lab[0],lab[1],lab[2])
def vis(xyz):
return colour.is_within_visible_spectrum(xyz)
print("sys.executable = "+sys.executable)
#print(vis(colour.xyY_to_XYZ([0.05, 0.8, 0.05])))
#print(vis(colour.xyY_to_XYZ([0.05, 0.8, 0.55])))
llimits = 1000
RECURSION_DEPTH = 16 # precision: 1/2^(n+1) because chromahigh is set to 0.5. 9 seems to be the absolute minimum acceptable precision for llimits=100
climits = [0]*(llimits+1)
H = float(sys.argv[1]) #153
if (H - int(sys.argv[1]) == 0):
H = int(sys.argv[1])
filename = "ChromaLimits-H"+str(H)+"-Lcnt"+str(llimits)+"-Clim"+str(2**(RECURSION_DEPTH+1))+".txt"
print("filename: "+filename)
for l in range(1,llimits): # 1..99
lightness = l / llimits
sys.stdout.write("Chroma limits for L="+str(lightness)+", H="+str(H)+"... ")
chromalow = 0.0
chromahigh = 0.5
chromamid = -1.0
recursion_count = 0
while recursion_count < RECURSION_DEPTH:
chromamid = (chromalow + chromahigh) / 2.0
col = lch_to_xyz(lightness, chromamid, H)
if (vis(col)):
chromalow = chromamid
else:
chromahigh = chromamid
recursion_count += 1
climits[l] = chromamid
sys.stdout.write(str(chromamid)+"\n")
print("Chroma limits by Lightness for H="+str(H)+":")
print(climits)
with open(filename, "w") as f:
numbers = ''.join(map(lambda x: str(x)+",", climits))
f.writelines('"'+str(H)+'"'+":["+numbers[0:-1]+"],")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment