File: __patch__.m

package info (click to toggle)
octave 10.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,484 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (198 lines) | stat: -rw-r--r-- 5,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
########################################################################
##
## Copyright (C) 2007-2025 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn {} {[@var{h}, @var{fail}] =} __patch__ (@var{p}, @dots{})
## Undocumented internal function.
## @end deftypefn

## __patch__ (p, x, y, c)
## Create patch object from x and y with color c and parent p.
## Return handle to patch object.

function h = __patch__ (p, varargin)

  h = NaN;
  nargin = nargin - 1;

  is_numeric_arg = cellfun (@isnumeric, varargin);

  if (isempty (varargin))
    args = {};
  elseif (is_numeric_arg(1))
    if (nargin < 3 || ! is_numeric_arg(2))
      print_usage ("patch");
    endif

    if (nargin >= 4 && all (is_numeric_arg(1:4)))
      x = varargin{1};
      y = varargin{2};
      z = varargin{3};
      c = varargin{4};
      iarg = 5;
    elseif (nargin >= 3 && all (is_numeric_arg(1:3)))
      x = varargin{1};
      y = varargin{2};
      if (nargin > 3 && iscolorspec (varargin{4}))
        z = varargin{3};
        c = varargin{4};
        iarg = 5;
      else
        z = [];
        c = varargin{3};
        iarg = 4;
      endif
    elseif (nargin >= 3 && all (is_numeric_arg(1:2)))
      x = varargin{1};
      y = varargin{2};
      z = [];
      if (iscolorspec (varargin{3}))
        c = varargin{3};
        iarg = 4;
      elseif (nargin == 3)
        error ("patch: invalid color specification C");
      else
        c = [];
        iarg = 3;
      endif
    endif

    if (isvector (x))
      x = x(:);
      y = y(:);
      z = z(:);
      if (isnumeric (c))
        if (isvector (c))
          if (isequal (size (c), [1, 3]))
            ## Do nothing, this is a single RGB color specification
          elseif (numel (c) == numel (x))
            c = c(:);
          endif
        elseif (rows (c) != numel (x) && columns (c) == numel (x))
          c = c.';
        endif
      endif
    endif
    args{1} = "xdata";
    args{2} = x;
    args{3} = "ydata";
    args{4} = y;
    args{5} = "zdata";
    args{6} = z;

    if (isnumeric (c) && ! isempty (c))

      if (ndims (c) == 3 && columns (c) == 1)
        c = permute (c, [1, 3, 2]);
      endif

      if (isvector (c) && numel (c) == columns (x))
        ## One color per face
        if (isnan (c))
          args{7} = "facecolor";
          args{8} = [1, 1, 1];
        else
          args{7} = "facecolor";
          args{8} = "flat";
        endif
        args{9} = "facevertexcdata";
        args{10} = c(:);
      elseif (isrow (c) && numel (c) == 3)
        ## One RGB color
        args{7} = "facecolor";
        args{8} = c;
        args{9} = "cdata";
        args{10} = [];
      elseif (ndims (c) == 3 && size (c, 3) == 3)
        ## CDATA is specified as 3-D RGB data
        if ((rows (c) == 1 && columns (c) == 1) ...
            || (rows (c) == 1 && columns (c) == columns (x)))
          ## Single patch color or per-face color
          args{7} = "facecolor";
          args{8} = "flat";
          args{9} = "cdata";
          args{10} = c;
        elseif (rows (c) == rows (x) && columns (c) == columns (x))
          ## Per-vertex color
          args{7} = "facecolor";
          args{8} = "interp";
          args{9} = "cdata";
          args{10} = c;
        else
          error ("patch: invalid TrueColor data C");
        endif
      else
        ## Color vectors
        if (isempty (c))
          args{7} = "facecolor";
          args{8} = "interp";
          args{9} = "cdata";
          args{10} = [];
        elseif (size_equal (c, x) && size_equal (c, y))
          args{7} = "facecolor";
          args{8} = "interp";
          args{9} = "cdata";
          args{10} = c;
        else
          error ("patch: size of X, Y, and C must be equal");
        endif
      endif
    elseif (iscolorspec (c))
      ## Color specification is a string
      args{7} = "facecolor";
      args{8} = lower (c);
      args{9} = "cdata";
      args{10} = [];
    elseif (! isempty (c))
      error ("patch: invalid color specification C");
    endif

    args = [args, varargin(iarg:end)];

  else
    ## "Property"/Value pair input
    args = varargin;
  endif

  if (isempty (p))
    p = gca ();
  endif
  h = __go_patch__ (p, args{:});

endfunction

function retval = iscolorspec (arg)

  retval = false;
  if (ischar (arg))
    persistent colors = {"y", "yellow", "r", "red", "m", "magenta", ...
                         "c", "cyan", "g", "green", "b", "blue", ...
                         "w", "white", "k", "black"};
    if (any (strcmpi (arg, colors)))
      retval = true;
    endif
  endif

endfunction