Skip to content

Commit 21b5692

Browse files
feat(bar, scatter): support color names and short color names (#1348)
1 parent 2f19696 commit 21b5692

File tree

10 files changed

+78
-18
lines changed

10 files changed

+78
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Major types conversions are available.
1818
- CMake: Optional Julia engine detection.
1919

20+
- `bar`, `scatter` manage color name and short colorname.
2021
- Github CI Ubuntu 24.04 arm64 (Cobalt 100 processor).
2122
- Github CI Snapcraft build amd64 and arm64.
2223
- Snapcraft arm64.

modules/graphics/functions/bar.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
defaultWidth = 0.8;
4242
defaultColor = getColorAndUpdateIndex(parent);
4343

44-
supportedColorString = string(getColorShortNameList());
44+
supportedColorString = [string(getColorShortNameList()), string(getColorNameList())];
4545
isString = @(x) (ischar(x) || isStringScalar(x)) && ~matches(convertCharsToStrings(x), supportedColorString);
4646
firstString = find (cellfun(isString, inputArguments), 1);
4747
if (isempty(firstString))
@@ -77,7 +77,7 @@
7777
color = defaultColor;
7878
X = inputArguments{1};
7979
X = X(:)';
80-
if isscalar(inputArguments{2})
80+
if isscalar(inputArguments{2}) || ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
8181
if isnumeric(inputArguments{2})
8282
width = inputArguments{2};
8383
elseif ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
@@ -92,7 +92,7 @@
9292
if isnumeric(inputArguments{3})
9393
width = inputArguments{3};
9494
elseif ischar(inputArguments{3}) || isStringScalar(inputArguments{3})
95-
color = convertStringsToChars(inputArguments{3});
95+
color = getColorShortName(convertStringsToChars(inputArguments{3}));
9696
else
9797
error(_('Unrecognized option for third argument.'));
9898
end
@@ -101,11 +101,11 @@
101101
function [X, Y, width, color] = parseTwoArguments(inputArguments, defaultWidth, defaultColor)
102102
width = defaultWidth;
103103
color = defaultColor;
104-
if isscalar(inputArguments{2})
104+
if isscalar(inputArguments{2}) || ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
105105
if isnumeric(inputArguments{2})
106106
width = inputArguments{2};
107107
elseif ischar(inputArguments{2}) || isStringScalar(inputArguments{2})
108-
color = convertStringsToChars(inputArguments{2})
108+
color = getColorShortName(convertStringsToChars(inputArguments{2}));
109109
else
110110
error(_('Unrecognized option for second argument.'));
111111
end

modules/graphics/functions/colstyle.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
orig_t = style;
5252
while (~giveup && length(style)>0)
5353
giveup = 1;
54-
if (matchit(style, getColorShortNameList()))
55-
[colorspec, style] = parseit(style, getColorShortNameList());
54+
if (matchit(getColorShortName(style), getColorShortNameList()))
55+
[colorspec, style] = parseit(getColorShortName(style), getColorShortNameList());
5656
giveup = 0;
5757
end;
5858
if (matchit(style, getMarkerNameList()))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%=============================================================================
2+
% Copyright (c) 2016-present Allan CORNET (Nelson)
3+
%=============================================================================
4+
% This file is part of the Nelson.
5+
%=============================================================================
6+
% LICENCE_BLOCK_BEGIN
7+
% SPDX-License-Identifier: LGPL-3.0-or-later
8+
% LICENCE_BLOCK_END
9+
%=============================================================================
10+
function l = getColorNameList()
11+
l = {'yellow','magenta','cyan','red','green','blue','white','black'};
12+
end
13+
%=============================================================================
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
%=============================================================================
2+
% Copyright (c) 2016-present Allan CORNET (Nelson)
3+
%=============================================================================
4+
% This file is part of the Nelson.
5+
%=============================================================================
6+
% LICENCE_BLOCK_BEGIN
7+
% SPDX-License-Identifier: LGPL-3.0-or-later
8+
% LICENCE_BLOCK_END
9+
%=============================================================================
10+
function l = getColorShortName(name)
11+
shortnames = getColorShortNameList();
12+
names = getColorNameList();
13+
idx = find(strcmpi(names, name), 1);
14+
if ~isempty(idx)
15+
l = shortnames{idx};
16+
else
17+
l = name;
18+
end
19+
end
20+
%=============================================================================
21+

modules/graphics/functions/scatter.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
nbInputArguments = length(inputArguments);
145145
end
146146

147-
supportedColorString = string(getColorShortNameList());
147+
supportedColorString = [string(getColorShortNameList()), string(getColorNameList())];
148148
isString = @(x) (ischar(x) || isStringScalar(x)) && ~matches(convertCharsToStrings(x), supportedColorString);
149149
firstString = find (cellfun(isString, inputArguments), 1);
150150
if (isempty(firstString))
@@ -156,7 +156,7 @@
156156
if ~isempty(inputArguments{3})
157157
markerSize = sqrt(inputArguments{3});
158158
end
159-
markerColor = inputArguments{4};
159+
markerColor = getColorShortName(inputArguments{4});
160160
inputArguments = inputArguments(5:end);
161161
nbInputArguments = nbInputArguments - 4;
162162
elseif (firstString > 3)

modules/graphics/help/en_US/xml/bar.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<param_input_item>
3737
<param_name>color</param_name>
3838
<param_description
39-
>a scalar string or row vector character: short name color.</param_description>
39+
>a scalar string or row vector character: color name or short color name.</param_description>
4040
</param_input_item>
4141

4242
<param_input_item>
@@ -163,6 +163,12 @@ bar(y, 'FaceColor', [0 .5 .5], 'EdgeColor', [0 .9 .9], 'LineWidth', 1.5)
163163
<history_version>1.0.0</history_version>
164164
<history_description>initial version</history_description>
165165
</history_item>
166+
<history_item>
167+
<history_version>1.12.0</history_version>
168+
<history_description
169+
>Color name or short color name managed.</history_description>
170+
</history_item>
171+
166172
</history>
167173

168174
<authors>

modules/graphics/help/en_US/xml/scatter.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<param_input_item>
3535
<param_name>c</param_name>
3636
<param_description
37-
>Marker color: color name, RGB triplet or vector of colormap indices</param_description>
37+
>Marker color: short color name, color name, RGB triplet or vector of colormap indices</param_description>
3838
</param_input_item>
3939
<param_input_item>
4040
<param_name>ax</param_name>
@@ -89,13 +89,13 @@
8989
<p />
9090

9191
<p>The ColorSpec specifies the marker color to use for each data series:</p>
92-
<p><b>'k'</b>: Color Black</p>
93-
<p><b>'y'</b>: Color Yellow</p>
94-
<p><b>'m'</b>: Color Magenta</p>
95-
<p><b>'c'</b>: Color Cyan</p>
96-
<p><b>'r'</b>: Color Red</p>
97-
<p><b>'b'</b>: Color Blue</p>
98-
<p><b>'g'</b>: Color Green</p>
92+
<p><b>'k'</b>, <b>'black'</b>: Color Black</p>
93+
<p><b>'y'</b>, <b>'yellow'</b>: Color Yellow</p>
94+
<p><b>'m'</b>, <b>'magenta'</b>: Color Magenta</p>
95+
<p><b>'c'</b>, <b>'cyan'</b>: Color Cyan</p>
96+
<p><b>'r'</b>, <b>'red'</b>: Color Red</p>
97+
<p><b>'b'</b>, <b>'blue'</b>: Color Blue</p>
98+
<p><b>'g'</b>, <b>'green'</b>: Color Green</p>
9999
<p />
100100
<p>see <b>line</b> for more information about properties</p>
101101
</description>
@@ -215,7 +215,14 @@ s = scatter(x,y, 'filled');
215215
<history_item>
216216
<history_version>1.0.0</history_version>
217217
<history_description>initial version</history_description>
218+
</history_item>
219+
<history_item>
220+
<history_version>1.12.0</history_version>
221+
<history_description
222+
>color name and short color name managed.</history_description>
218223
</history_item>
224+
225+
219226
</history>
220227

221228
<authors>

modules/graphics/tests/test_bar.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@
2929
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
3030
bar(y,'r')
3131
%=============================================================================
32+
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
33+
bar(y,'green')
34+
%=============================================================================
3235
x = 1900:10:2000;
3336
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
3437
bar(x, y, 'r');
3538
%=============================================================================
39+
x = 1900:10:2000;
40+
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
41+
bar(x, y, 'cyan');
42+
%=============================================================================
3643
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
3744
bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
3845
%=============================================================================

modules/graphics/tests/test_scatter.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
h = scatter (x, y, [], 'r', 's');
9191
%=============================================================================
9292
f = figure();
93+
x = [0.777753, 0.093848, 0.183162, 0.399499, 0.337997, 0.686724, 0.073906, 0.651808, 0.869273, 0.137949];
94+
y = [0.37460, 0.25027, 0.19510, 0.51182, 0.54704, 0.56087, 0.24853, 0.75443, 0.42712, 0.44273];
95+
h = scatter (x, y, [], 'green', 's');
96+
%=============================================================================
97+
f = figure();
9398
x = rand(100,1);
9499
y = rand(100,1);
95100
MarkerColor = [0.5, 0.5, 0.5];

0 commit comments

Comments
 (0)