benjamin moore colors

Author

Haky Im

Published

January 1, 2020

Read benjamin moore list of colors with HEX code from colornerd

Code
suppressMessages(library(tidyverse))

# Install and load the jsonlite package
if (!require(jsonlite)) install.packages("jsonlite")
Loading required package: jsonlite

Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':

    flatten
Code
suppressMessages(library(jsonlite))

# Define the URL of the JSON file
#url <- "https://raw.githubusercontent.com/wesbos/benjamin-moore-css/main/colors.json"
url <- "https://raw.githubusercontent.com/hakyim/benjamin-moore-css/main/colors.json"

# Read the JSON file from the URL
data <- fromJSON(url) %>% as_tibble()

# Function to convert hex to RGB
hex_to_rgb <- function(hex) {
  hex <- paste0("#", hex)  # Ensure the hex value has a '#' prefix
  rgb <- col2rgb(hex)
  list(r = rgb[1, 1], g = rgb[2, 1], b = rgb[3, 1])
}

# Function to calculate LRV from RGB
calculate_lrv <- function(hex) {
  rgb <- hex_to_rgb(hex)
  lrv_trucho <- (0.2126 * rgb$r + 0.7152 * rgb$g + 0.0722 * rgb$b) / 255 * 100
  return(lrv_trucho)
}

# Calculate LRV for each color
data <- data %>%
  mutate(lrv_trucho = map_dbl(hex, calculate_lrv))

# show properties of my selected colors
data %>% filter(name %in% c("Classic Gray","Gray Owl","Abalone")) %>% select(name, hex, lrv_trucho, number, family) %>% arrange(name)
# A tibble: 5 × 5
  name         hex    lrv_trucho number  family 
  <chr>        <chr>       <dbl> <chr>   <chr>  
1 Abalone      D3CFC7       81.3 2108-60 Gray   
2 Classic Gray E3E0D7       87.8 1548    Neutral
3 Classic Gray E3E0D7       87.8 OC-23   Gray   
4 Gray Owl     D3D4CC       82.8 2137-60 Neutral
5 Gray Owl     D3D4CC       82.8 OC-52   Neutral