Skip to content

Instantly share code, notes, and snippets.

@glenndevenish
Last active October 1, 2018 09:31
Show Gist options
  • Save glenndevenish/1ad6013a8c07beba22720d1902589b70 to your computer and use it in GitHub Desktop.
Save glenndevenish/1ad6013a8c07beba22720d1902589b70 to your computer and use it in GitHub Desktop.
Gets RGB, HEX, and CMYK values from a Pantone
/*
How to use:
Go to pantone.com
open console
paste the below function
Run with getPantoneData('123 c').then(...)
It returns an object with the format:
{
pantone: '123-C',
rgb: [255, 199, 44],
hex: 'FFC72C',
cmyk: [0, 19, 98, 0]
}
*/
function getPantoneData(pantone) { // pantone in format '123-C', '124 cp', etc
const colourString = pantone.trim().replace(/ /g, '-').toLocaleUpperCase() // format string to url end
return fetch('https://www.pantone.com/color-finder/' + colourString)
.then(r=>r.text())
.then(data => {
stripped = data.replace(/\s*/g, '')
regex = /spanclass="values">([A-Z0-9]{6}|\d+)/g // Regex to find hex/numbers from the html string
arr = []
while(match = regex.exec(stripped)) { // = assigns, rather than == (catches nulls)
arr.push(match[1])
}
return {
pantone: colourString.replace('-', ' '),
rgb: [arr[0], arr[1], arr[2]],
hex: arr[3],
cmyk: [arr[4], arr[5], arr[6], arr[7]]
}
})
}
@glenndevenish
Copy link
Author

glenndevenish commented Aug 10, 2018

Basic usage:

getPantoneData('123 c').then(console.log)

Get hex values from array of pantones:

pantones = ['123 c', '124 c', '456 c']

Promise.all(pantones.map(pantone => getPantoneData(pantone)))
.then(responses => {
  responses.forEach(response => console.log('#' + response.hex))
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment