<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/a-very-palette-able-post/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/05/21/a-very-pallete-able-post/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Many of my posts seem to begin with a link to a tweet, and this one falls into that pattern:</p>
<blockquote data-lang="en" class="twitter-tweet"><p dir="ltr" lang="en">And <a target="_blank" rel="nofollow" href="https://twitter.com/_inundata"><a target="_blank" rel="nofollow" href="http://twitter.com/_inundata">@_inundata</a></a> is already working on a <a target="_blank" rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash">#rstats</a> palette. <a target="_blank" rel="nofollow" href="https://t.co/bNfpL7OmVl">https://t.co/bNfpL7OmVl</a></p>
<p>— Timothée Poisot (<a target="_blank" rel="nofollow" href="http://twitter.com/tpoi">@tpoi</a>) <a target="_blank" rel="nofollow" href="https://twitter.com/tpoi/status/866136213174071296">May 21, 2017</a></p></blockquote>
<p></p>
<p>I’d seen the <a target="_blank" rel="nofollow" href="https://arstechnica.com/information-technology/2017/05/an-ai-invented-a-bunch-of-new-paint-colors-that-are-hilariously-wrong/">Ars Tech post</a> about the named color palette derived from some training data. I could tell at a glance of the resultant palette:</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/05/tumblr_inline_opgsh0UI6N1rl9zu7_400.png?ssl=1"><img alt="" class="wp-image-5966" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/05/tumblr_inline_opgsh0UI6N1rl9zu7_400.png?resize=249%2C557&ssl=1" data-recalc-dims="1"></a></p>
<p>that it would not be ideal for visualizations (use <a target="_blank" rel="nofollow" href="http://www.color-blindness.com/coblis-color-blindness-simulator/">this site</a> test the final image in this post and verify that on your own) but this was a neat, quick project to take on, especially since it let me dust off an old GH package, <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/adobecolor"><code>adobecolor</code></a> and it was likely I could beat Karthik to creating a palette <img alt="😉" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f609.png"></p>
<p>The “B+” goal is to get a color palette that “matches” the one in the Tumlbr post. The “A” goal is to get a named palette.</p>
<p>These are all the packages we end up using:</p>
<pre><code>library(tesseract)
library(magick)
library(stringi)
library(adobecolor) # hrbrmstr/adobecolor - may not be Windows friendly
library(tidyverse)</code></pre>
<h3>Attempt #1 (B+!!)</h3>
<p>I’m a macOS user, so I’ve got great tools like <a target="_blank" rel="nofollow" href="http://xscopeapp.com/">xScope</a> at my disposal. I’m <em>really</em> handy with that app and the Loupe tool makes it easy to point at a color, save it to a palette board and export an ACO palette file. </p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/05/Loupe.png?ssl=1"><img alt="" class="wp-image-5967" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/05/Loupe.png?resize=300%2C284&ssl=1" data-recalc-dims="1"></a></p>
<p>That whole process took ~18 seconds (first try). I’m not saying that to brag. But we often get hung up on both speed and programmatic reproducibility. I ultimately — as we’ll see in a bit — really went for speed vs programmatic reproducibility.</p>
<p>It’s dead simple to get the palette into R:</p>
<pre><code>aco_fil &lt;- &quot;ml_cols.aco&quot;
aco_hex &lt;- rev(read_aco(aco_fil))

col2rgb(aco_hex)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## red    112  203   97  191  120  221  169  233  177   216    62   178   199
## green  112  198   92  174  114  196  167  191  138   200    63   184   172
## blue    85  166   73  156  124  199  171  143  109   185    67   196   146
##       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
## red      48   172   177   203   219   162   152   232   197   191
## green    94   152   100   205   210    98   165   177   161   161
## blue     83   145   107   192   179   106   158   135   171   124</code></pre>
<p>IIRC there may still be a byte-order issue (PRs welcome) I need to deal with on Windows in <code>adobecolor</code> but you likely will never need to use the package again.</p>
<p>A quick eyeball comparison between the Tumblr list and that matrix indicates the colors are off. That could be for many reasons starting from the way they were encoded in the PNG by whatever programming language was used to train the neural net and make the image (likely Python) to Tumblr degrading it to something on my end. You’ll see that the colors are close enough for humans that it’s likely close enough.</p>
<p>There, I’ve got a B+ with about a total of 60s of work! Plenty of time left to try shooting for an A!</p>
<h3>Attempt #2 (FAIL)</h3>
<p>We’ve got the PNG from the Tumblr post and the <code>tesseract</code> package in R. Perhaps this will be super-quick, too:</p>
<pre><code>pal_img_fil &lt;- &quot;tumblr_inline_opgsh0UI6N1rl9zu7_400.png&quot;

pal_ocr &lt;- ocr(pal_img_fil)
stri_split_lines(pal_ocr)
## [[1]]
##  [1] &quot;-ClaniicFug112113 84&quot;      &quot;-Snowhnn.k 201 199165&quot;    
##  [3] &quot;- Cmbabcl 97 93 68&quot;        &quot;-Bunﬂuw 190 174 155&quot;      
##  [5] &quot;-an:hing Blue 121 114125&quot;  &quot;Bank Bun 221 196199&quot;      
##  [7] &quot;- Caring Tan 171 166170&quot;   &quot;-Smrguun 233191 141&quot;      
##  [9] &quot;-Sink 176 131; 110&quot;        &quot;Slummy Beige 216 200135&quot;  
## [11] &quot;- Durkwumi 61 63 66&quot;       &quot;Flow/£1178 1114 196&quot;      
## [13] &quot;- Sand Dan 2111 172143&quot;    &quot;- Grade 136: 41; 94 x3&quot;   
## [15] &quot;-Ligh[OfBlasll75150147&quot;    &quot;-Grass 13m 176 99108&quot;     
## [17] &quot;Sindis Poop 204 205 194&quot;   &quot;Dupe 219 2119179&quot;         
## [19] &quot;-'n:sling156101 106&quot;       &quot;-SloncrElu13152165 159&quot;   
## [21] &quot;- Buxblc Simp 226 1x1 132&quot; &quot;-Sl.mky 13m197162171&quot;     
## [23] &quot;-'J\\milyl90164116&quot;        &quot;&quot;                         
## [25] &quot;&quot;</code></pre>
<p>Ugh.</p>
<p>Perhaps if we crop out the colors:</p>
<pre><code>image_read(pal_img_fil) %>%
  image_crop(&quot;+57&quot;) %>%
  ocr() %>%
  stri_split_lines()
## [[1]]
##  [1] &quot;Clanﬁc Fug112113 84&quot;       &quot;Snowhunk 201 199 165&quot;     
##  [3] &quot;Cmbabcl 97 93 as&quot;          &quot;Bunﬂuwl90174155&quot;          
##  [5] &quot;Kunming Blue 121 114 125&quot;  &quot;Bank Bun 221196199&quot;       
##  [7] &quot;Caring Tan 171 ms 170&quot;     &quot;Slarguun 233 191 141&quot;     
##  [9] &quot;Sinkl76135110&quot;             &quot;&quot;                         
## [11] &quot;SIIImmy Beige 216 200 135&quot; &quot;Durkwuud e1 63 66&quot;        
## [13] &quot;Flower 175 154 196&quot;        &quot;&quot;                         
## [15] &quot;Sand Dan 201 172 143&quot;      &quot;Grade 1m AB 94: 53&quot;       
## [17] &quot;&quot;                          &quot;Light 0mm 175 150 147&quot;    
## [19] &quot;Grass Ba! 17a 99 ms&quot;       &quot;sxndis Poop 204 205 194&quot;  
## [21] &quot;Dupe 219 209 179&quot;          &quot;&quot;                         
## [23] &quot;Tesling 156 101 106&quot;       &quot;SloncrEluc 152 165 159&quot;   
## [25] &quot;Buxblc Simp 226 131 132&quot;   &quot;Sumky Bean 197 162 171&quot;   
## [27] &quot;1\\mﬂy 190 164 11a&quot;        &quot;&quot;                         
## [29] &quot;&quot;</code></pre>
<p>Ugh.</p>
<p>I’m woefully unfamiliar with how to use the plethora of tesseract options to try to get better performance and this is taking too much time for a toy post, so we’ll call this attempt a failure <img alt="🙁" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f641.png"></p>
<h3>Attempt #3 (A-!!)</h3>
<p>I’m going to go outside of R again to <a target="_blank" rel="nofollow" href="http://www.newocr.com/">New OCR</a> and upload the Tumblr palette there and crop out the colors (it lets you do that in-browser). NOTE: Never use any free site for OCR’ing sensitive data as most are run by content thieves. </p>
<p><em>Now</em> we’re talkin’:</p>
<pre><code>ocr_cols &lt;- &quot;Clardic Fug 112 113 84
Snowbonk 201 199 165
Catbabel 97 93 68
Bunfiow 190 174 155
Ronching Blue 121 114 125
Bank Butt 221 196 199
Caring Tan 171 166 170
Stargoon 233 191 141
Sink 176 138 110
Stummy Beige 216 200 185
Dorkwood 61 63 66
Flower 178 184 196
Sand Dan 201 172 143
Grade Bat 48 94 83
Light Of Blast 175 150 147
Grass Bat 176 99 108
Sindis Poop 204 205 194
Dope 219 209 179
Testing 156 101 106
Stoncr Blue 152 165 159
Burblc Simp 226 181 132
Stanky Bean 197 162 171
Thrdly 190 164 116&quot;</code></pre>
<p>We can get that into a more useful form pretty quickly:</p>
<pre><code>stri_match_all_regex(ocr_cols, &quot;([[:alpha:] ]+) ([[:digit:]]+) ([[:digit:]]+) ([[:digit:]]+)&quot;) %>%
  print() %>%
  .[[1]] -> col_mat
## [[1]]
##       [,1]                         [,2]             [,3]  [,4]  [,5] 
##  [1,] &quot;Clardic Fug 112 113 84&quot;     &quot;Clardic Fug&quot;    &quot;112&quot; &quot;113&quot; &quot;84&quot; 
##  [2,] &quot;Snowbonk 201 199 165&quot;       &quot;Snowbonk&quot;       &quot;201&quot; &quot;199&quot; &quot;165&quot;
##  [3,] &quot;Catbabel 97 93 68&quot;          &quot;Catbabel&quot;       &quot;97&quot;  &quot;93&quot;  &quot;68&quot; 
##  [4,] &quot;Bunfiow 190 174 155&quot;        &quot;Bunfiow&quot;        &quot;190&quot; &quot;174&quot; &quot;155&quot;
##  [5,] &quot;Ronching Blue 121 114 125&quot;  &quot;Ronching Blue&quot;  &quot;121&quot; &quot;114&quot; &quot;125&quot;
##  [6,] &quot;Bank Butt 221 196 199&quot;      &quot;Bank Butt&quot;      &quot;221&quot; &quot;196&quot; &quot;199&quot;
##  [7,] &quot;Caring Tan 171 166 170&quot;     &quot;Caring Tan&quot;     &quot;171&quot; &quot;166&quot; &quot;170&quot;
##  [8,] &quot;Stargoon 233 191 141&quot;       &quot;Stargoon&quot;       &quot;233&quot; &quot;191&quot; &quot;141&quot;
##  [9,] &quot;Sink 176 138 110&quot;           &quot;Sink&quot;           &quot;176&quot; &quot;138&quot; &quot;110&quot;
## [10,] &quot;Stummy Beige 216 200 185&quot;   &quot;Stummy Beige&quot;   &quot;216&quot; &quot;200&quot; &quot;185&quot;
## [11,] &quot;Dorkwood 61 63 66&quot;          &quot;Dorkwood&quot;       &quot;61&quot;  &quot;63&quot;  &quot;66&quot; 
## [12,] &quot;Flower 178 184 196&quot;         &quot;Flower&quot;         &quot;178&quot; &quot;184&quot; &quot;196&quot;
## [13,] &quot;Sand Dan 201 172 143&quot;       &quot;Sand Dan&quot;       &quot;201&quot; &quot;172&quot; &quot;143&quot;
## [14,] &quot;Grade Bat 48 94 83&quot;         &quot;Grade Bat&quot;      &quot;48&quot;  &quot;94&quot;  &quot;83&quot; 
## [15,] &quot;Light Of Blast 175 150 147&quot; &quot;Light Of Blast&quot; &quot;175&quot; &quot;150&quot; &quot;147&quot;
## [16,] &quot;Grass Bat 176 99 108&quot;       &quot;Grass Bat&quot;      &quot;176&quot; &quot;99&quot;  &quot;108&quot;
## [17,] &quot;Sindis Poop 204 205 194&quot;    &quot;Sindis Poop&quot;    &quot;204&quot; &quot;205&quot; &quot;194&quot;
## [18,] &quot;Dope 219 209 179&quot;           &quot;Dope&quot;           &quot;219&quot; &quot;209&quot; &quot;179&quot;
## [19,] &quot;Testing 156 101 106&quot;        &quot;Testing&quot;        &quot;156&quot; &quot;101&quot; &quot;106&quot;
## [20,] &quot;Stoncr Blue 152 165 159&quot;    &quot;Stoncr Blue&quot;    &quot;152&quot; &quot;165&quot; &quot;159&quot;
## [21,] &quot;Burblc Simp 226 181 132&quot;    &quot;Burblc Simp&quot;    &quot;226&quot; &quot;181&quot; &quot;132&quot;
## [22,] &quot;Stanky Bean 197 162 171&quot;    &quot;Stanky Bean&quot;    &quot;197&quot; &quot;162&quot; &quot;171&quot;
## [23,] &quot;Thrdly 190 164 116&quot;         &quot;Thrdly&quot;         &quot;190&quot; &quot;164&quot; &quot;116&quot;</code></pre>
<p>The <code>print()</code> is in the pipe as I can never remember where each <code>stringi</code> functions stick lists but usually guess right, plus I wanted to check the output.</p>
<p>Making those into colors is super-simple:</p>
<pre><code>y &lt;- apply(col_mat[,3:5], 2, as.numeric)

ocr_cols &lt;- rgb(y[,1], y[,2], y[,3], names=col_mat[,2], maxColorValue = 255)</code></pre>
<p>If we look at Attempt #1 and Attempt #2 together:</p>
<pre><code>ocr_cols
##    Clardic Fug       Snowbonk       Catbabel        Bunfiow  Ronching Blue 
##      &quot;#707154&quot;      &quot;#C9C7A5&quot;      &quot;#615D44&quot;      &quot;#BEAE9B&quot;      &quot;#79727D&quot; 
##      Bank Butt     Caring Tan       Stargoon           Sink   Stummy Beige 
##      &quot;#DDC4C7&quot;      &quot;#ABA6AA&quot;      &quot;#E9BF8D&quot;      &quot;#B08A6E&quot;      &quot;#D8C8B9&quot; 
##       Dorkwood         Flower       Sand Dan      Grade Bat Light Of Blast 
##      &quot;#3D3F42&quot;      &quot;#B2B8C4&quot;      &quot;#C9AC8F&quot;      &quot;#305E53&quot;      &quot;#AF9693&quot; 
##      Grass Bat    Sindis Poop           Dope        Testing    Stoncr Blue 
##      &quot;#B0636C&quot;      &quot;#CCCDC2&quot;      &quot;#DBD1B3&quot;      &quot;#9C656A&quot;      &quot;#98A59F&quot; 
##    Burblc Simp    Stanky Bean         Thrdly 
##      &quot;#E2B584&quot;      &quot;#C5A2AB&quot;      &quot;#BEA474&quot;

aco_hex
##  [1] &quot;#707055&quot; &quot;#CBC6A6&quot; &quot;#615C49&quot; &quot;#BFAE9C&quot; &quot;#78727C&quot; &quot;#DDC4C7&quot; &quot;#A9A7AB&quot;
##  [8] &quot;#E9BF8F&quot; &quot;#B18A6D&quot; &quot;#D8C8B9&quot; &quot;#3E3F43&quot; &quot;#B2B8C4&quot; &quot;#C7AC92&quot; &quot;#305E53&quot;
## [15] &quot;#AC9891&quot; &quot;#B1646B&quot; &quot;#CBCDC0&quot; &quot;#DBD2B3&quot; &quot;#A2626A&quot; &quot;#98A59E&quot; &quot;#E8B187&quot;
## [22] &quot;#C5A1AB&quot; &quot;#BFA17C&quot;</code></pre>
<p>we can see they’re <em>really</em> close to each other, and I doubt all but the most egregiously picky color snobs can tell the difference visually, too:</p>
<pre><code>par(mfrow=c(1,2))
scales::show_col(ocr_cols)
scales::show_col(aco_hex)
par(mfrow=c(1,1))</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/05/pals2-1.png?ssl=1"><img alt="" class="wp-image-5974" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/05/pals2-1.png?resize=780%2C390&ssl=1" data-recalc-dims="1"></a></p>
<p>(OK, <code>#3D3F43</code> is definitely hitting my OCD as being annoyingly different than <code>#3D3F42</code> on my MacBook Pro so count me in as a color snob.)</p>
<p>Here’s the final palette:</p>
<pre><code>structure(c(&quot;#707154&quot;, &quot;#C9C7A5&quot;, &quot;#615D44&quot;, &quot;#BEAE9B&quot;, &quot;#79727D&quot;, 
&quot;#DDC4C7&quot;, &quot;#ABA6AA&quot;, &quot;#E9BF8D&quot;, &quot;#B08A6E&quot;, &quot;#D8C8B9&quot;, &quot;#3D3F42&quot;, 
&quot;#B2B8C4&quot;, &quot;#C9AC8F&quot;, &quot;#305E53&quot;, &quot;#AF9693&quot;, &quot;#B0636C&quot;, &quot;#CCCDC2&quot;, 
&quot;#DBD1B3&quot;, &quot;#9C656A&quot;, &quot;#98A59F&quot;, &quot;#E2B584&quot;, &quot;#C5A2AB&quot;, &quot;#BEA474&quot;
), .Names = c(&quot;Clardic Fug&quot;, &quot;Snowbonk&quot;, &quot;Catbabel&quot;, &quot;Bunfiow&quot;, 
&quot;Ronching Blue&quot;, &quot;Bank Butt&quot;, &quot;Caring Tan&quot;, &quot;Stargoon&quot;, &quot;Sink&quot;, 
&quot;Stummy Beige&quot;, &quot;Dorkwood&quot;, &quot;Flower&quot;, &quot;Sand Dan&quot;, &quot;Grade Bat&quot;, 
&quot;Light Of Blast&quot;, &quot;Grass Bat&quot;, &quot;Sindis Poop&quot;, &quot;Dope&quot;, &quot;Testing&quot;, 
&quot;Stoncr Blue&quot;, &quot;Burblc Simp&quot;, &quot;Stanky Bean&quot;, &quot;Thrdly&quot;))</code></pre>
<p>This third attempt took ~5 minutes vs 60s.</p>
<h3>FIN</h3>
<p>Why “A-“? Well, I didn’t completely verify the colors and values matched 100% in the final submission. They are likely the same, but the best way to get something corrected by others it to put it on the internet, so <em>there it is</em> <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<p>I’d be a better human and coder if I took the time to learn <code>tesseract</code> more, but I don’t have much need for OCR’ing text. It is likely worth the time to brush up on <code>tesseract</code> after you read this post. </p>
<p><em>Don’t use this palette</em>! I created it mostly to beat Karthik to making the palette (I have no idea if I succeeded), to also show that you should not forego your base R roots (I could have let that be subliminal but I wasn’t trying to socially engineer you in this post) and to bring up the speed/reproducibility topic. I see no issues with manually doing tasks (like uploading an image to a web site) in certain circumstances, but it’d be an interesting topic of debate to see just what “rules” folks use to determine how much effort one should put into 100% programmatic reproducibility.</p>
<p>You can find the ACO file and an earlier, alternate attempt at making the palette in <a target="_blank" rel="nofollow" href="https://gist.github.com/hrbrmstr/1082a9b28120a22f6a7cf9e8ecae2eef">this gist</a>. </p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2017/05/21/a-very-pallete-able-post/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/tbuz940VIGQ"></a></div>