Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use getTextColor for retention heatmap cells so text is visible on dark backgrounds #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 31 additions & 56 deletions alamode.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ var alamode = {
return "." + id;
},

getTextColor: function(hex) {
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}

var isHex = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(hex);

if (isHex) {
rgb = hexToRgb(hex);
o = Math.round(((parseInt(rgb.r) * 299) + (parseInt(rgb.g) * 587) + (parseInt(rgb.b) * 114)) /1000);
} else {
o = 255;
}

if (o > 125) {
return "#2B2B2B";
} else {
return "#FCFCFC";
}
},

addLinksToTables: function(o) {
var tableId = "#" + o["table_id"],
linkColumns = o["link_columns"],
Expand Down Expand Up @@ -524,6 +550,7 @@ var alamode = {
.data(function(d) { return makeRow(data,d); })
.enter().append("td")
.style("background",function(d) { if (checkShade(d)) { return pickColor(d); } })
.style("color",function(d) { if (checkShade(d)) { return alamode.getTextColor(pickColor(d)); } })
.attr("class",function(d) { return cellClass(d); })
.text(function(d) { return fmt(d,o); })

Expand Down Expand Up @@ -2844,7 +2871,7 @@ var alamode = {
data.forEach(function(d,i) {
var selector = tableId + " table [data-axel-rowkey='" + i + "'][data-axel-column='" + idx + "']",
selectedColor = scale(d[column]),
textColor = getTextColor(selectedColor),
textColor = alamode.getTextColor(selectedColor),
cell = $(selector);

if (colorText) { cell.css("color",selectedColor); } else { cell.css( {"background":selectedColor,"color":textColor} ); }
Expand All @@ -2854,7 +2881,7 @@ var alamode = {
function drawThreshold(column, type, threshold, color, colorText) {

var idx = colIndex[column];
var textColor = getTextColor(color);
var textColor = alamode.getTextColor(color);

data.forEach(function(d,i) {
var selector = tableId + " table [data-axel-rowkey='" + i + "'][data-axel-column='" + idx + "']",
Expand All @@ -2869,32 +2896,6 @@ var alamode = {
}
})
}

function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}

function getTextColor(hex) {
var isHex = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(hex);

if (isHex) {
rgb = hexToRgb(hex);
o = Math.round(((parseInt(rgb.r) * 299) + (parseInt(rgb.g) * 587) + (parseInt(rgb.b) * 114)) /1000);
} else {
o = 255;
}

if (o > 125) {
return "#2B2B2B";
} else {
return "#FCFCFC";
}
}
},

customizeTable: function (o) {
Expand Down Expand Up @@ -2972,7 +2973,7 @@ var alamode = {

var selector = tableId + " table [data-axel-rowkey='" + i + "'][data-axel-column='" + idx + "']",
selectedColor = scale(d[c]),
textColor = getTextColor(selectedColor),
textColor = alamode.getTextColor(selectedColor),
cell = $(selector);

if (colorText) { cell.css("color",selectedColor); } else { cell.css( {"background":selectedColor,"color":textColor} ); }
Expand All @@ -2982,7 +2983,7 @@ var alamode = {

function drawThreshold(type, threshold, color, colorText) {

var textColor = getTextColor(color);
var textColor = alamode.getTextColor(color);

data.forEach(function(d,i) {
includedColumns.forEach(function(c) {
Expand All @@ -3003,32 +3004,6 @@ var alamode = {
})
})
}

function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}

function getTextColor(hex) {
var isHex = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(hex);

if (isHex) {
rgb = hexToRgb(hex);
o = Math.round(((parseInt(rgb.r) * 299) + (parseInt(rgb.g) * 587) + (parseInt(rgb.b) * 114)) /1000);
} else {
o = 255;
}

if (o > 125) {
return "#2B2B2B";
} else {
return "#FCFCFC";
}
}
},

addTableOfContents: function(o){
Expand Down
Loading