How do I check if a value is a correct color?

9 ビュー (過去 30 日間)
Ivan
Ivan 2014 年 7 月 16 日
回答済み: Paul Wintz 2021 年 8 月 20 日
Whether there is a function like
logical = isvalidcolor(value)
to check if some value may be used like a color (one of values specified in ColorSpec e.g.)

採用された回答

Image Analyst
Image Analyst 2014 年 7 月 16 日
Not that I'm aware of - there's just not that much need - though you could write one if you want.
  4 件のコメント
Ivan
Ivan 2014 年 7 月 30 日
It's a good question! I havn't known about announcing an error by uicontrol or some else.
Image Analyst
Image Analyst 2014 年 7 月 30 日
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;

サインインしてコメントする。

その他の回答 (2 件)

Tommy
Tommy 2015 年 12 月 6 日
編集済み: Tommy 2015 年 12 月 6 日
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 件のコメント
Grzegorz Lippe
Grzegorz Lippe 2021 年 4 月 26 日
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 :/
Paul Wintz
Paul Wintz 2021 年 8 月 20 日
Matlab 2020b has a validatecolor function.

サインインしてコメントする。


Paul Wintz
Paul Wintz 2021 年 8 月 20 日
In Matlab 2020b and later, use the validatecolor function.

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by