{"id":5291,"date":"2022-04-09T00:01:07","date_gmt":"2022-04-09T07:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5291"},"modified":"2022-04-02T11:52:25","modified_gmt":"2022-04-02T18:52:25","slug":"color-text-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5291","title":{"rendered":"Color Text, Part II"},"content":{"rendered":"<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270&#038;preview=true\">last week&#8217;s Lesson<\/a>, I showed how ANSI codes are used to set color text in terminal output. It&#8217;s time to go nuts showing the possibilities.<br \/>\n<!--more--><br \/>\nI should write, &#8220;go loopy&#8221; instead of &#8220;go nuts,&#8221; as the sample code in this Lesson involves plenty of loops. The goal is to output a spectrum of color values, eight foreground and eight background.<\/p>\n<p>The foreground colors range from 30 through 37. Background colors range from 40 through 47. Rather than explain what each value represents, why not just look at the output? First the foreground codes:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_04_09-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_04_09-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int c;\r\n    const char *colors[] = {\r\n        \"BLACK\", \"RED\", \"GREEN\", \"YELLOW\",\r\n        \"BLUE\", \"MAGENTA\", \"CYAN\", \"WHITE\"\r\n    };\r\n\r\n    for( c=0 ; c&lt;8; c++ )\r\n    {\r\n        printf(\"%10s: \\x1b[3%cm%s\\x1b[m\\n\",\r\n                colors[c],\r\n                c+'0',\r\n                colors[c]\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A <em>const char<\/em> array <code>colors[]<\/code> contains the text for the color names corresponding to codes zero through seven. These are output in the <em>for<\/em> loop at Line 11.<\/p>\n<p>The <em>printf()<\/em> statement at Line 13 outputs the color text name first in standard terminal colors. (My terminal&#8217;s standard colors are orange on black.) Next, the ANSI escape sequence is cobbled together. The value <code>c+'0'<\/code> sets the ASCII character value for the color code. This value is inserted into the ANSI escape sequence to generate the proper foreground color. Also stuffed into the <em>printf()<\/em> format string is the &#8220;normal&#8221; ASNI escape sequence, <code>\\x1b[m<\/code>, which disables the color attribute after the text is output. Figure 1 shows the code&#8217;s sample output.<\/p>\n<div id=\"attachment_5295\" style=\"width: 338px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5295\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/03\/0409-fig1.png\" alt=\"\" width=\"328\" height=\"298\" class=\"size-full wp-image-5295\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/03\/0409-fig1.png 328w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/03\/0409-fig1-300x273.png 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><p id=\"caption-attachment-5295\" class=\"wp-caption-text\">Figure 1. Color text output on my terminal, which uses an orange text foreground and black background.<\/p><\/div>\n<p>The first line of output in Figure 1 shows black text on a black background, which makes the text appear invisible though it&#8217;s still output. It&#8217;s important to properly coordinate foreground and background colors, not only to keep things visible but to ensure that the color pairing doesn&#8217;t nauseate the user.<\/p>\n<p>To assist you in choosing the best colors, the following code outputs the entire 64 possible foreground and background color combinations.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_04_09-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_04_09-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int f,b;\r\n\r\n\r\n    for( f=0 ; f&lt;8; f++ )\r\n    {\r\n        for( b=0; b&lt;8; b++ )\r\n        {\r\n            printf(\"\\x1b[4%c;3%cm %02d:%02d \",\r\n                    f+'0',b+'0',\r\n                    f,b\r\n                  );\r\n        }\r\n        printf(\"\\x1b[m\\n\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The nested <em>for<\/em> loops use variables <code>f<\/code> for the foreground color values and <code>b<\/code> for the background color values. The <em>printf()<\/em> statement at Line 12 assembles the proper ASNI escape sequence, setting both values in the sequence, separated by a semicolon. The code pair values are also output.<\/p>\n<p>The <em>printf()<\/em> statement at Line 17 resets text attributes, ensuring that any &#8220;bleed&#8221; is removed between lines. Figure 2 shows the sample output on my terminal screen.<\/p>\n<div id=\"attachment_5297\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5297\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0409-fig2.png\" alt=\"Color output grid\" width=\"500\" height=\"165\" class=\"size-full wp-image-5297\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0409-fig2.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0409-fig2-300x99.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-5297\" class=\"wp-caption-text\">Figure 1. The full color grid, foreground and background color combinations.<\/p><\/div>\n<p>In Figure 2, the first value in a pair represents the foreground color, which are shown as columns. The second value represents the background, which appear in rows. The diagonal from 00:00 through 07:07 appears invisible as the foreground and background colors are identical.<\/p>\n<p>Other ASNI code values can be mixed with the color values used in this Lesson. In fact, the full slate of colors increases to 16-by-16 when you add the &#8220;intensity&#8221; value. This ASNI escape sequence is <code>\\x1b[1m,<\/code> with code &#8216;1&#8217; indicating bold or &#8220;intense&#8221; text. The bold attribute may not work on all terminals.<\/p>\n<p>For the full list of ASNI codes, refer to <a href=\"https:\/\/en.wikipedia.org\/wiki\/ANSI_escape_code\" rel=\"noopener\" target=\"_blank\">this page on Wikipedia<\/a>. The codes relevant to this post are found in the section titled &#8220;SGR (Select Graphic Rendition) parameters.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to go nuts with color text. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5291\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5291","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5291"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5291\/revisions"}],"predecessor-version":[{"id":5307,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5291\/revisions\/5307"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}