{"id":4027,"date":"2020-03-10T07:00:36","date_gmt":"2020-03-10T11:00:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=4027"},"modified":"2020-08-18T09:09:48","modified_gmt":"2020-08-18T13:09:48","slug":"how-to-display-color-swatches","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2020\/03\/10\/how-to-display-color-swatches\/","title":{"rendered":"How to Display Color Swatches"},"content":{"rendered":"<div class=\"content\"><p>When working on the \"Color Image Processing\" chapter of <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a>, I found myself often wanting to display square blocks (or swatches) of color, like this:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/line-order-colors.png\" alt=\"\"> <\/p><p>Eventually, I wrote a function, <tt>colorSwatches<\/tt>, to display a bunch of color squares using a single patch object. This function is used in <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a>, and it is included in the <a href=\"https:\/\/github.com\/dipum\/dipum-toolbox\">MATLAB code files for the book<\/a>. To call it, start with a set of RGB color values arranged in a Px3 matrix, where P is the number of colors. The <tt>lines<\/tt> functions returns the colors used by the MATLAB plot function:<\/p><pre class=\"codeinput\">c = lines(7)\r\n<\/pre><pre class=\"codeoutput\">\r\nc =\r\n\r\n         0    0.4470    0.7410\r\n    0.8500    0.3250    0.0980\r\n    0.9290    0.6940    0.1250\r\n    0.4940    0.1840    0.5560\r\n    0.4660    0.6740    0.1880\r\n    0.3010    0.7450    0.9330\r\n    0.6350    0.0780    0.1840\r\n\r\n<\/pre><p>Now just call <tt>colorSwatches<\/tt>, passing in the set of colors, and optionally passing in the desired size of the color grid.<\/p><pre class=\"codeinput\">grid_size = [3 3];\r\ncolorSwatches(c,grid_size)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_01.png\" alt=\"\"> <p>The color swatches in the above figure are not quite square, and that's because the data aspect ratio in the axes is not 1:1. But we can easily change that using the <tt>daspect<\/tt> function.<\/p><pre class=\"codeinput\">daspect([1 1 1])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_02.png\" alt=\"\"> <p>Let's look under the hood and see this all works using just a single patch object.<\/p><pre class=\"codeinput\">p = colorSwatches(c,grid_size)\r\n<\/pre><pre class=\"codeoutput\">\r\np = \r\n\r\n  Patch with properties:\r\n\r\n    FaceColor: 'flat'\r\n    FaceAlpha: 1\r\n    EdgeColor: 'none'\r\n    LineStyle: '-'\r\n        Faces: [9&times;5 double]\r\n     Vertices: [45&times;2 double]\r\n\r\n  Use GET to show all properties\r\n\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_03.png\" alt=\"\"> <p>For us, the key properties are <tt>Vertices<\/tt>, <tt>Faces<\/tt>, and <tt>FaceVertexCData<\/tt>. The <tt>Vertices<\/tt> property is 45x2, indicating that the patch has 45 vertices. Here they are:<\/p><pre class=\"codeinput\">hold <span class=\"string\">on<\/span>\r\nplot(p.Vertices(:,1),p.Vertices(:,2),<span class=\"string\">'*'<\/span>)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_04.png\" alt=\"\"> <p>The <tt>Faces<\/tt> property is 9x5, indicating that there are 9 faces, each of which connects 5 vertices. Here is the second row of the <tt>Faces<\/tt> property:<\/p><pre class=\"codeinput\">face2 = p.Faces(2,:)\r\n<\/pre><pre class=\"codeoutput\">\r\nface2 =\r\n\r\n     6     7     8     9    10\r\n\r\n<\/pre><p>These are indices into the <tt>Vertices<\/tt> property. Below, I'll use the <tt>Faces<\/tt> and <tt>Vertices<\/tt> properties to draw a black line around the second face.<\/p><pre class=\"codeinput\">x = p.Vertices(face2,1);\r\ny = p.Vertices(face2,2);\r\nhold <span class=\"string\">on<\/span>\r\nplot(x,y,<span class=\"string\">'k'<\/span>,<span class=\"string\">'LineWidth'<\/span>,3)\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_05.png\" alt=\"\"> <p>The <tt>FaceVertexCData<\/tt> property controls the color of each face. Note that each face has a constant color because the <tt>FaceColor<\/tt> property is <tt>'flat'<\/tt>.<\/p><pre class=\"codeinput\">p.FaceVertexCData\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n         0    0.4470    0.7410\r\n    0.8500    0.3250    0.0980\r\n    0.9290    0.6940    0.1250\r\n    0.4940    0.1840    0.5560\r\n    0.4660    0.6740    0.1880\r\n    0.3010    0.7450    0.9330\r\n    0.6350    0.0780    0.1840\r\n       NaN       NaN       NaN\r\n       NaN       NaN       NaN\r\n\r\n<\/pre><p>If we change the numbers on the second row of the <tt>FaceVertexCData<\/tt> property, then the color of the second face will change.<\/p><pre class=\"codeinput\">p.FaceVertexCData(2,:) = [1 1 0];\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_06.png\" alt=\"\"> <p>Another application of <tt>colorSwatches<\/tt> is to draw a color ramp. For example, we can visualize the parula colormap by using a single row of color swatches, setting the gap between each swatch to 0, and controlling the data aspect ratio to make it look like a long, thin bar.<\/p><pre class=\"codeinput\">parula_colors = parula(256);\r\ngap = 0;\r\ncolorSwatches(parula_colors,gap,[1 256])\r\ndaspect([30 1 1])\r\naxis <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_07.png\" alt=\"\"> <p>One final note: when I wrote <tt>colorSwatches<\/tt>, I didn't want it to have any side effects on the axes properties, and that's why I didn't write it to automatically set the axes <tt>DataAspectRatio<\/tt>. I do think, though, that having color swatches that are actually square is sufficiently common to write a function for it, so I have added <tt>squareColorSwatches<\/tt> to <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64161-matlab-color-tools\"><i>MATLAB Color Tools<\/i><\/a>. Here it is in action:<\/p><pre class=\"codeinput\">squareColorSwatches(c,[3 3])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_08.png\" alt=\"\"> <script language=\"JavaScript\"> <!-- \r\n    function grabCode_a5ad294c8dac4934ad5cf09fc52926ac() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a5ad294c8dac4934ad5cf09fc52926ac ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a5ad294c8dac4934ad5cf09fc52926ac';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2020 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a5ad294c8dac4934ad5cf09fc52926ac()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2019b<br><\/p><\/div><!--\r\na5ad294c8dac4934ad5cf09fc52926ac ##### SOURCE BEGIN #####\r\n%%\r\n% When working on the \"Color Image Processing\" chapter of\r\n% <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\r\n% *DIPUM3E*>, I found myself often wanting to display square blocks (or\r\n% swatches) of color, like this:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/line-order-colors.png>>\r\n%\r\n% Eventually, I wrote a function, |colorSwatches|, to display a bunch of\r\n% color squares using a single patch object. This function is used in\r\n% <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\r\n% *DIPUM3E*>, and it is included in the\r\n% <https:\/\/github.com\/dipum\/dipum-toolbox MATLAB code files for the book>.\r\n% To call it, start with a set of RGB color values arranged in a Px3\r\n% matrix, where P is the number of colors. The |lines| functions returns\r\n% the colors used by the MATLAB plot function:\r\n\r\nc = lines(7)\r\n\r\n%%\r\n% Now just call |colorSwatches|, passing in the set of colors, and\r\n% optionally passing in the desired size of the color grid.\r\n\r\ngrid_size = [3 3];\r\ncolorSwatches(c,grid_size)\r\n\r\n%%\r\n% The color swatches in the above figure are not quite square, and that's\r\n% because the data aspect ratio in the axes is not 1:1. But we can easily\r\n% change that using the |daspect| function.\r\n\r\ndaspect([1 1 1])\r\n\r\n%%\r\n% Let's look under the hood and see this all works using just a single\r\n% patch object.\r\n\r\np = colorSwatches(c,grid_size)\r\n\r\n%%\r\n% For us, the key properties are |Vertices|, |Faces|, and\r\n% |FaceVertexCData|. The |Vertices| property is 45x2, indicating that the\r\n% patch has 45 vertices. Here they are:\r\n\r\nhold on\r\nplot(p.Vertices(:,1),p.Vertices(:,2),'*')\r\nhold off\r\n\r\n%%\r\n% The |Faces| property is 9x5, indicating that there are 9 faces, each of\r\n% which connects 5 vertices. Here is the second row of the |Faces|\r\n% property:\r\n\r\nface2 = p.Faces(2,:)\r\n\r\n%%\r\n% These are indices into the |Vertices| property. Below, I'll use the\r\n% |Faces| and |Vertices| properties to draw a black line around the second\r\n% face.\r\n\r\nx = p.Vertices(face2,1);\r\ny = p.Vertices(face2,2);\r\nhold on\r\nplot(x,y,'k','LineWidth',3)\r\nhold off\r\n\r\n%%\r\n% The |FaceVertexCData| property controls the color of each face. Note that\r\n% each face has a constant color because the |FaceColor| property is\r\n% |'flat'|.\r\n\r\np.FaceVertexCData\r\n\r\n%%\r\n% If we change the numbers on the second row of the |FaceVertexCData|\r\n% property, then the color of the second face will change.\r\n\r\np.FaceVertexCData(2,:) = [1 1 0];\r\n\r\n%%\r\n% Another application of |colorSwatches| is to draw a color ramp. For\r\n% example, we can visualize the parula colormap by using a single row of\r\n% color swatches, setting the gap between each swatch to 0, and\r\n% controlling the data aspect ratio to make it look like a long, thin bar.\r\n\r\nparula_colors = parula(256);\r\ngap = 0;\r\ncolorSwatches(parula_colors,gap,[1 256])\r\ndaspect([30 1 1])\r\naxis off\r\n\r\n%%\r\n% One final note: when I wrote |colorSwatches|, I didn't want it to have\r\n% any side effects on the axes properties, and that's why I didn't write it\r\n% to automatically set the axes |DataAspectRatio|. I do think, though, that\r\n% having color swatches that are actually square is sufficiently common to\r\n% write a function for it, so I have added |squareColorSwatches| to <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64161-matlab-color-tools\r\n% _MATLAB Color Tools_>. Here it is in action:\r\n\r\nsquareColorSwatches(c,[3 3])\r\n##### SOURCE END ##### a5ad294c8dac4934ad5cf09fc52926ac\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/color_swatches_08.png\" onError=\"this.style.display ='none';\" \/><\/div><p>When working on the \"Color Image Processing\" chapter of DIPUM3E, I found myself often wanting to display square blocks (or swatches) of color, like this: Eventually, I wrote a function,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/03\/10\/how-to-display-color-swatches\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[214,90,1308,1093,68],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4027"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=4027"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4027\/revisions"}],"predecessor-version":[{"id":4059,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4027\/revisions\/4059"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=4027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=4027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=4027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}