How do I check if a value is a correct color?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ivan
am 16 Jul. 2014
Beantwortet: Paul Wintz
am 20 Aug. 2021
Whether there is a function like
logical = isvalidcolor(value)
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 16 Jul. 2014
Not that I'm aware of - there's just not that much need - though you could write one if you want.
4 Kommentare
Image Analyst
am 30 Jul. 2014
You could do something like
message = sprintf('%s is an invalid color. Please use r, g, b, y, m, c, k, or w', theirColor);
uiwait(warndlg(message));
return;
Weitere Antworten (2)
Tommy
am 6 Dez. 2015
Bearbeitet: Tommy
am 6 Dez. 2015
Once I needed this function to suppress error messages, by setting wrong color values to MATLAB default. I used:
function logical_out = iscolor( color )
color = lower(color);
logical_out=(isnumeric(color) && (sum(size(color)==[1 3])==2 || ...
sum(size(color)==[3 1])==2) && sum((color<=[1 1 1] & ...
color>=[0 0 0]))==3) || sum(strcmpi({'y','m','c','r','g','b','w','k',...
'yellow','magenta','cyan','red','green','blue','white','black'},color))>0;
end
Hope this helps.
6 Kommentare
Grzegorz Lippe
am 26 Apr. 2021
Well that helps until that point somebody specifies the color as hexadecimal value or integer from 0 ...255. Hope mathworks creates a validate color function :/
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!