color names in 'set color' command

This commit is contained in:
Ian Jauslin 2022-02-18 17:51:13 -05:00
parent 57fb407dea
commit 19838bc2a2
2 changed files with 22 additions and 32 deletions

View File

@ -2,10 +2,10 @@
def closest_color(rgb,names):
name=""
mindist=float('inf')
for i in range(len(names)):
dist=(rgb[0]-names[i][1])**2+(rgb[1]-names[i][2])**2+(rgb[2]-names[i][3])**2
for color in names:
dist=(rgb[0]-color[1])**2+(rgb[1]-color[2])**2+(rgb[2]-color[3])**2
if dist<mindist:
name=names[i][0]
name=color[0]
mindist=dist
return name

View File

@ -5,6 +5,7 @@ import glob
import os.path
import filecheck
import colors
class Command_prompt(Label):
@ -337,36 +338,25 @@ class Command_prompt(Label):
if len(argv)<3:
self.message="error: 'set color' command was run with without anargument -- usage: 'set color <color_descriptor>'"
return
if argv[2]=="blue":
self.app.painter.set_color((0,0,1))
elif argv[2]=="green":
self.app.painter.set_color((0,1,0))
elif argv[2]=="red":
self.app.painter.set_color((1,0,0))
elif argv[2]=="cyan":
self.app.painter.set_color((0,1,1))
elif argv[2]=="magenta":
self.app.painter.set_color((1,0,1))
elif argv[2]=="yellow":
self.app.painter.set_color((1,1,0))
elif argv[2]=="white":
self.app.painter.set_color((1,1,1))
elif argv[2]=="black":
self.app.painter.set_color((0,0,0))
elif argv[2]=="gray":
self.app.painter.set_color((0.5,0.5,0.5))
else:
color_str=argv[2].split(",")
# error if improperly formatted
if len(color_str)!=3:
self.message="error: unrecognized color specification '"+argv[2]+"'; supported format is 'r,g,b' or one of red|green|blue|cyan|magenta|yellow|white|black|gray"
# find color name in list
for color in colors.xcolor_names:
if argv[2]==color[0]:
self.app.painter.set_color(color[1:])
return
try:
color=(float(color_str[0]),float(color_str[1]),float(color_str[2]))
except:
self.message="error: unrecognized color specification '"+argv[2]+"'; supported format is 'r,g,b' or one of red|green|blue|cyan|magenta|yellow|white|black|gray"
return
self.app.painter.set_color(color)
# numerical rgb values
color_str=argv[2].split(",")
# error if improperly formatted
if len(color_str)!=3:
self.message="error: unrecognized color specification '"+argv[2]+"'; supported format is 'r,g,b' or one of red|green|blue|brown|lime|orange|pink|purple|teal|violet|cyan|magenta|yellow|olive|black|darkgray|gray|lightgray|white or an SVG color name"
return
try:
color=(float(color_str[0]),float(color_str[1]),float(color_str[2]))
except:
self.message="error: unrecognized color specification '"+argv[2]+"'; supported format is 'r,g,b' or one of red|green|blue|brown|lime|orange|pink|purple|teal|violet|cyan|magenta|yellow|olive|black|darkgray|gray|lightgray|white or an SVG color name"
return
self.app.painter.set_color(color)
# toggle grid
def run_set_grid(self,argv):