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

View File

@ -5,6 +5,7 @@ import glob
import os.path import os.path
import filecheck import filecheck
import colors
class Command_prompt(Label): class Command_prompt(Label):
@ -337,36 +338,25 @@ class Command_prompt(Label):
if len(argv)<3: if len(argv)<3:
self.message="error: 'set color' command was run with without anargument -- usage: 'set color <color_descriptor>'" self.message="error: 'set color' command was run with without anargument -- usage: 'set color <color_descriptor>'"
return return
if argv[2]=="blue": # find color name in list
self.app.painter.set_color((0,0,1)) for color in colors.xcolor_names:
elif argv[2]=="green": if argv[2]==color[0]:
self.app.painter.set_color((0,1,0)) self.app.painter.set_color(color[1:])
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"
return return
try:
color=(float(color_str[0]),float(color_str[1]),float(color_str[2])) # numerical rgb values
except: color_str=argv[2].split(",")
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" # error if improperly formatted
return if len(color_str)!=3:
self.app.painter.set_color(color) 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 # toggle grid
def run_set_grid(self,argv): def run_set_grid(self,argv):