Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bar, scatter): support color names and short color names #1348

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Major types conversions are available.
- CMake: Optional Julia engine detection.

- `bar`, `scatter` manage color name and short colorname.
- Github CI Ubuntu 24.04 arm64 (Cobalt 100 processor).
- Github CI Snapcraft build amd64 and arm64.
- Snapcraft arm64.
Expand Down
10 changes: 5 additions & 5 deletions modules/graphics/functions/bar.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
defaultWidth = 0.8;
defaultColor = getColorAndUpdateIndex(parent);

supportedColorString = string(getColorShortNameList());
supportedColorString = [string(getColorShortNameList()), string(getColorNameList())];
isString = @(x) (ischar(x) || isStringScalar(x)) && ~matches(convertCharsToStrings(x), supportedColorString);
firstString = find (cellfun(isString, inputArguments), 1);
if (isempty(firstString))
Expand Down Expand Up @@ -77,7 +77,7 @@
color = defaultColor;
X = inputArguments{1};
X = X(:)';
if isscalar(inputArguments{2})
if isscalar(inputArguments{2}) || ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
if isnumeric(inputArguments{2})
width = inputArguments{2};
elseif ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
Expand All @@ -92,7 +92,7 @@
if isnumeric(inputArguments{3})
width = inputArguments{3};
elseif ischar(inputArguments{3}) || isStringScalar(inputArguments{3})
color = convertStringsToChars(inputArguments{3});
color = getColorShortName(convertStringsToChars(inputArguments{3}));
else
error(_('Unrecognized option for third argument.'));
end
Expand All @@ -101,11 +101,11 @@
function [X, Y, width, color] = parseTwoArguments(inputArguments, defaultWidth, defaultColor)
width = defaultWidth;
color = defaultColor;
if isscalar(inputArguments{2})
if isscalar(inputArguments{2}) || ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
if isnumeric(inputArguments{2})
width = inputArguments{2};
elseif ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
color = convertStringsToChars(inputArguments{2})
color = getColorShortName(convertStringsToChars(inputArguments{2}));
else
error(_('Unrecognized option for second argument.'));
end
Expand Down
4 changes: 2 additions & 2 deletions modules/graphics/functions/colstyle.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
orig_t = style;
while (~giveup && length(style)>0)
giveup = 1;
if (matchit(style, getColorShortNameList()))
[colorspec, style] = parseit(style, getColorShortNameList());
if (matchit(getColorShortName(style), getColorShortNameList()))
[colorspec, style] = parseit(getColorShortName(style), getColorShortNameList());
giveup = 0;
end;
if (matchit(style, getMarkerNameList()))
Expand Down
13 changes: 13 additions & 0 deletions modules/graphics/functions/private/getColorNameList.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
%=============================================================================
% Copyright (c) 2016-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function l = getColorNameList()
l = {'yellow','magenta','cyan','red','green','blue','white','black'};
end
%=============================================================================
21 changes: 21 additions & 0 deletions modules/graphics/functions/private/getColorShortName.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%=============================================================================
% Copyright (c) 2016-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function l = getColorShortName(name)
shortnames = getColorShortNameList();
names = getColorNameList();
idx = find(strcmpi(names, name), 1);
if ~isempty(idx)
l = shortnames{idx};
else
l = name;
end
end
%=============================================================================

4 changes: 2 additions & 2 deletions modules/graphics/functions/scatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
nbInputArguments = length(inputArguments);
end

supportedColorString = string(getColorShortNameList());
supportedColorString = [string(getColorShortNameList()), string(getColorNameList())];
isString = @(x) (ischar(x) || isStringScalar(x)) && ~matches(convertCharsToStrings(x), supportedColorString);
firstString = find (cellfun(isString, inputArguments), 1);
if (isempty(firstString))
Expand All @@ -156,7 +156,7 @@
if ~isempty(inputArguments{3})
markerSize = sqrt(inputArguments{3});
end
markerColor = inputArguments{4};
markerColor = getColorShortName(inputArguments{4});
inputArguments = inputArguments(5:end);
nbInputArguments = nbInputArguments - 4;
elseif (firstString > 3)
Expand Down
8 changes: 7 additions & 1 deletion modules/graphics/help/en_US/xml/bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<param_input_item>
<param_name>color</param_name>
<param_description
>a scalar string or row vector character: short name color.</param_description>
>a scalar string or row vector character: color name or short color name.</param_description>
</param_input_item>

<param_input_item>
Expand Down Expand Up @@ -163,6 +163,12 @@ bar(y, 'FaceColor', [0 .5 .5], 'EdgeColor', [0 .9 .9], 'LineWidth', 1.5)
<history_version>1.0.0</history_version>
<history_description>initial version</history_description>
</history_item>
<history_item>
<history_version>1.12.0</history_version>
<history_description
>Color name or short color name managed.</history_description>
</history_item>

</history>

<authors>
Expand Down
23 changes: 15 additions & 8 deletions modules/graphics/help/en_US/xml/scatter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<param_input_item>
<param_name>c</param_name>
<param_description
>Marker color: color name, RGB triplet or vector of colormap indices</param_description>
>Marker color: short color name, color name, RGB triplet or vector of colormap indices</param_description>
</param_input_item>
<param_input_item>
<param_name>ax</param_name>
Expand Down Expand Up @@ -89,13 +89,13 @@
<p />

<p>The ColorSpec specifies the marker color to use for each data series:</p>
<p><b>'k'</b>: Color Black</p>
<p><b>'y'</b>: Color Yellow</p>
<p><b>'m'</b>: Color Magenta</p>
<p><b>'c'</b>: Color Cyan</p>
<p><b>'r'</b>: Color Red</p>
<p><b>'b'</b>: Color Blue</p>
<p><b>'g'</b>: Color Green</p>
<p><b>'k'</b>, <b>'black'</b>: Color Black</p>
<p><b>'y'</b>, <b>'yellow'</b>: Color Yellow</p>
<p><b>'m'</b>, <b>'magenta'</b>: Color Magenta</p>
<p><b>'c'</b>, <b>'cyan'</b>: Color Cyan</p>
<p><b>'r'</b>, <b>'red'</b>: Color Red</p>
<p><b>'b'</b>, <b>'blue'</b>: Color Blue</p>
<p><b>'g'</b>, <b>'green'</b>: Color Green</p>
<p />
<p>see <b>line</b> for more information about properties</p>
</description>
Expand Down Expand Up @@ -215,7 +215,14 @@ s = scatter(x,y, 'filled');
<history_item>
<history_version>1.0.0</history_version>
<history_description>initial version</history_description>
</history_item>
<history_item>
<history_version>1.12.0</history_version>
<history_description
>color name and short color name managed.</history_description>
</history_item>


</history>

<authors>
Expand Down
7 changes: 7 additions & 0 deletions modules/graphics/tests/test_bar.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'r')
%=============================================================================
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'green')
%=============================================================================
x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x, y, 'r');
%=============================================================================
x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x, y, 'cyan');
%=============================================================================
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
%=============================================================================
5 changes: 5 additions & 0 deletions modules/graphics/tests/test_scatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
h = scatter (x, y, [], 'r', 's');
%=============================================================================
f = figure();
x = [0.777753, 0.093848, 0.183162, 0.399499, 0.337997, 0.686724, 0.073906, 0.651808, 0.869273, 0.137949];
y = [0.37460, 0.25027, 0.19510, 0.51182, 0.54704, 0.56087, 0.24853, 0.75443, 0.42712, 0.44273];
h = scatter (x, y, [], 'green', 's');
%=============================================================================
f = figure();
x = rand(100,1);
y = rand(100,1);
MarkerColor = [0.5, 0.5, 0.5];
Expand Down
Loading