function MunsellSpecStrings = MunsellTreeToMunsellSpecifications(XYZ);
% Purpose Convert a Munsell specification, given in Cartesian coordinates in the Munsell
% tree, into standard Munsell notation (Hue Value/Chroma).
%
% Description The Munsell system specifies a local colour by giving its hue (H), value (V),
% and chroma(C) in the form H V/C. The value is a number between 0 and 10.
% The chroma is a positive number, whose bound depends on hue and value,
% as given by the MacAdam limits. The hue specification consists of a letter
% designator (B, BG, G, GY, Y, YR, R, RP, P, PB), and a number designator
% which is greater than or equal to 0, and less than or equal to 10. If chroma is
% 0, then the local colour has no hue, and is specified as NV, where N is the
% string "N," and V is the value. For example, 5.0R 9.0/2.0 is a light pastel
% red, while N3 is a dark grey.
%
% The Munsell system can be visualized geometrically as an irregular "tree,"
% which is a subset of cylindrical three-dimensional space. The vertical
% axis in this space is given by Munsell value. The horizontal plane of the
% space is in polar coordinates. The distance r from the origin corresponds
% to Munsell chroma. The angle theta corresponds to hue. Theta is chosen
% to be 0 along the x-axis, which has Munsell hue 0R, and to increase
% counterclockwise, going evenly through YR, Y, etc. until reaching RP and
% arriving back at R. While r and theta are natural coordinates in some
% circumstances, we will use standard Cartesian coordinates x and y, instead.
%
% This routine takes as input a matrix, each row of which is a set of Cartesian
% XYZ coordinates in the Munsell tree, and converts it to a list, indexed by {},
% of standard Munsell notations, such as 7Y 5/6. The conversion details are
% straightforward.
%
% XYZ A three-column matrix. Each row of the matrix gives the Cartesian
% XYZ coordinates in the Munsell tree, for one colour.
%
% MunsellSpecStrings A list, using {}, of standard Munsell specifications.
%
% Author Paul Centore (July 13, 2017)
%
% Copyright 2017 Paul Centore
%
% This file is part of MunsellAndKubelkaMunkToolbox.
%
% MunsellAndKubelkaMunkToolbox 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.
%
% MunsellAndKubelkaMunkToolbox 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 MunsellAndKubelkaMunkToolbox. If not, see .
% Initialize the return variable.
MunsellSpecStrings = {} ;
% Check that there are some inputs; otherwise, return with a message
if isempty(XYZ)
disp(['Exiting routine MunsellTreeToMunsellSpecifications: no inputs']) ;
return ;
end
NumberOfColours = size(XYZ,1) ;
% Convert each input colour to a standard Munsell notation
for ctr = 1:NumberOfColours
[HueAngleRadians, MunsellChroma, MunsellValue] = cart2pol(XYZ(ctr,1), XYZ(ctr,2), XYZ(ctr,3)) ;
HueAngleDegrees = rad2deg(HueAngleRadians) ;
ASTMhue = HueAngleDegrees/3.6 ;
[HueString, ~, ~] = ASTMHueToMunsellHue(ASTMhue, 6) ;
MunsellSpecStrings{ctr} = [HueString,' ',num2str(MunsellValue),'/',num2str(MunsellChroma)] ;
end