-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
269 lines (189 loc) · 6.72 KB
/
README.Rmd
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
## Overview
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/colornames)](https://CRAN.R-project.org/package=colornames)
<!-- badges: end -->
The **colornames** R package aims to convert color values into color names using various APIs:
- *[www.thecolorapi.com](https://www.thecolorapi.com)*: pass in any valid color and get conversion into any other format, the name of the color, placeholder images and a multitude of schemes.
- *[colornames.org](https://colornames.org)*: a collaborative effort to name every color in the RGB/web space. You can [name a random color](https://colornames.org/random/) or vote from some [incoming color names](https://colornames.org/fresh/).
- *[color.pizza](https://github.com/meodai/color-name-api)*: Rest API that returns a bunch of color names for a given color-value.
## Installation
```{r eval=FALSE}
install.packages("colornames")
#install.packages("devtools")
devtools::install_github("lgnbhl/colornames") # from GitHub
```
## Minimal examples
```{r}
library(colornames)
```
Color name using thecolorapi's API.
```{r}
get_color_thecolorapi(hex = "#0047AB")
```
Color name using colornames's API.
```{r}
get_color_colornames(hex = "#0047AB")
```
Color name using color.pizza's API.
```{r}
get_color_colorpizza(hex = "#0047AB")
```
## thecolorapi.com API
Call `get_color_thecolorapi()` to get the closest color name available of a hex code.
```{r}
get_color_thecolorapi(hex = "#0047AB")
```
The "thecolorapi.com" API return a list of variables about a given color name, by using `return_name_only = FALSE`.
```{r}
col1 <- get_color_thecolorapi(hex = "#0047AB", return_name_only = FALSE)
str(col1, max.level = 1)
```
You can create a data.frame from this list.
```{r}
as.data.frame(col1[1:9])
```
You can get color names from different color convention, i.e. `rgb`, `hsl` or `cmyk`.
```{r}
get_color_thecolorapi(rgb = paste(3,60,71, sep = ","))
get_color_thecolorapi(hsl = paste("100","100%","34%", sep = ","))
get_color_thecolorapi(cmyk = paste(50,58,0,33, sep = ","))
```
You can get color names from an existing list of colors using the closest named hex available.
```{r}
library(scales)
library(purrr)
pal_hue <- scales::brewer_pal()(6)
pal_names <- purrr::map_chr(.x = pal_hue, .f = get_color_thecolorapi)
data.frame(
hex = pal_hue,
name = pal_names
)
```
You can also get a color scheme (color palette) from any color.
```{r}
pal <- get_color_thecolorapi_scheme(
hex = "0047AB",
count = 5,
mode = "monochrome",
return_name_only = FALSE)
#get closed color name available
# data.frame(
# hex = pal$colors$hex$value,
# name = pal$colors$name$value
# )
pal$colors$hex
```
**Note that when an invalid color name is used, the API return "#000000" (black color).**
```{r}
get_color_thecolorapi(hex = "InvalidColorReturnsBlack")
```
## colornames.org API
```{r}
get_color_colornames(hex = "3D290C")
```
The function `get_color_colornames()` can also return a dataframe using `return_name_only = FALSE`.
**Note that the API returns hex codes as pure numbers, without a starting hashtag “\#”**
```{r}
df <- get_color_colornames(hex = "3D290C", return_name_only = FALSE)
#add "#" back in beginning of each hex code
df$hex <- paste0("#", df$hex)
df
```
Hex codes should **always** have 6 characters:
```{r, eval=FALSE}
get_color_colornames(hex = "440154FF")
```
# Error: Hex code 440154FF has 8 characters.
# Hex code should have 6 characters.
You an also get an random color name:
```{r}
get_color_colornames_random(return_name_only = FALSE)
```
You can explore the latest 100 submissions:
```{r}
latest_100 <- get_color_colornames_latest(return_name_only = FALSE)
head(latest_100)
```
You can loop to get color names of a given palette:
```{r}
library(scales)
library(purrr)
pal_hue <- scales::brewer_pal()(6)
pal_names <- purrr::map_chr(.x = pal_hue, .f = get_color_colornames)
data.frame(
hex = pal_hue,
name = pal_names
)
```
If color names are missing (like above), you can choose to [give them a name](https://colornames.org/random/).
If for some reason you want to use this color names database into
production, you should be aware that the color names are potentially
changing as [anyone can vote](https://colornames.org/fresh/) anytime to change any color name. Therefore you should download a copy of the colornames database at a given time.
You can download the complete data and use it locally.
```{r, eval=FALSE}
url_data <- "https://colornames.org/download/colornames.zip"
your_file_path <- paste0(getwd(), "/colornames-", Sys.Date(), ".zip")
download.file(url = url_data, destfile = your_file_path)
colornames_df <- readr::read_csv(your_file_path)
# Joke with hex codes 00000 to 00003
colornames_df
```
# # A tibble: 3,157,757 × 3
# hex bestName votes
# <chr> <chr> <dbl>
# 1 000000 Dude Turn The Lights Back On 5351
# 2 000001 It's Still Basically Black 729
# 3 000002 Still Black 121
# 4 000003 So Close To Black It Hurts 145
# 5 000004 Blackerererer 27
# 6 000005 Jet Black Heart 16
# 7 000006 Abaddon 16
# 8 000007 Double O Seven 56
# 9 000008 Closed Eyes 26
# 10 000009 Really Dark Blue 29
# # ℹ 3,157,747 more rows
This [downloadable data](https://colornames.org/download/) is totally free of rights, under [CC0
1.0](https://creativecommons.org/publicdomain/zero/1.0/).
## color.pizza API
The color.pizza database is more stable as it is modified only by its core contributors.
You can get the name of of the closest color name with `get_color_colorpizza()`.
```{r}
get_color_colorpizza(hex = "#3D290C")
```
You can access different information about a given color using `return_name_only = FALSE`.
```{r}
get_color_colorpizza(hex = "#3D290C", return_name_only = FALSE)
```
You can get the color names from a unique source with "list" argument and avoid duplicated colors with "noduplicates".
```{r}
get_color_colorpizza(
hex = c("#3D290C","#1c2f11","#2e3f24"),
list = "wikipedia",
noduplicates = TRUE,
return_name_only = FALSE)
```
You can get color names from an existing list of colors using the closest named hex available.
```{r}
library(purrr)
library(scales)
pal_hue <- scales::brewer_pal()(6)
pal_hue_names <- get_color_colorpizza(hex = pal_hue)
data.frame(
hex = pal_hue,
name = pal_hue_names
)
```