I have been looking for a way which can convert to HexColor into Color Name. I have searched online but no such thing is available online.
Your help will be much appreciated.
I have been looking for a way which can convert to HexColor into Color Name. I have searched online but no such thing is available online.
Your help will be much appreciated.
If I understand correctly what you would like to do is to get the color name of a cell in text based on the Hex code of that color.
I do not think it is possible using regular formulas from Google Sheets, so as a workaround I created the following script in Google Apps Script as a sample, so you can modify it according to what you would like to do.
let hexCode = [
{
color: "indian red",
code: {
hex: "#B0171F"
},
id: 1
},
{
color: "crimson ",
code: {
hex: "#DC143C"
},
id: 2
},
];
function getColorName(input) {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange('B2');
var bg = range.getBackground();
var colorIs;
for (var i = 0; i < hexCode.length; i++) {
if (colorIs = hexCode[i].code.hex.valueOf().toString().toLowerCase() === bg.valueOf()) {
return hexCode[i].color.valueOf();
}
}
}
Basically I created an array that contains the hex codes of the colors that I will be using and created a custom function so that when I call it in Google Sheets it gets the hex code of the color from the selected range and compares it to the list of colors from the array so that it returns the name of the color.
You can just modify the array adding the colors that you need using the same format and also change the range. I will also leave a screenshot so that you can see how it works.
For example, how about using an external API? In this sample script, the API of The Color API is used. The sample script is as follows.
In this sample, the hex data is converted to the color name using a custom function.
Please copy and paste the following script to the script editor of Google Spreadsheet and save it. When you use this script, for example, please put a custom function of =SAMPLE("ff0000") to a cell. By this, in this case, "Red" is returned.
const SAMPLE = hex => {
const res = UrlFetchApp.fetch("https://www.thecolorapi.com/id?format=json&hex=" + hex, { muteHttpExceptions: true });
if (res.getResponseCode() == 200) {
const obj = JSON.parse(res.getContentText());
return obj.name && obj.name.value ? obj.name.value : "";
}
return "";
}
In this sample, the background colors of cells retrieved from a1Notation are converted to the color names using a custom function.
Please copy and paste the following script to the script editor of Google Spreadsheet and save it.
const SAMPLE = a1Notation =>
SpreadsheetApp.getActiveSheet().getRange(a1Notation).getBackgrounds()
.map(r => r.map(c => {
const res = UrlFetchApp.fetch("https://www.thecolorapi.com/id?format=json&hex=" + c.slice(1), { muteHttpExceptions: true });
if (res.getResponseCode() == 200) {
const obj = JSON.parse(res.getContentText());
return obj.name && obj.name.value ? obj.name.value : "";
}
return "";
}));
When you use this script, please put the custom function of =SAMPLE("a1Notation") to a cell. The sample situation is as follows. In this sample, by =SAMPLE("A1:C3"), the background color names of "A1:C3" are retrieved.