Skip to content

Commit

Permalink
Properly merge colors for the label colors in the tooltip. I added a …
Browse files Browse the repository at this point in the history
…private helper to simplify the code in the tooltip
  • Loading branch information
etimberg committed Oct 8, 2016
1 parent 4d2772e commit abef7a2
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/core/core.tooltip.js
Expand Up @@ -4,6 +4,14 @@ module.exports = function(Chart) {

var helpers = Chart.helpers;

/**
* Helper method to merge the opacity into a color
*/
function mergeOpacity(colorString, opacity) {
var color = helpers.color(colorString);
return color.alpha(opacity * color.alpha()).rgbaString();
}

Chart.defaults.global.tooltips = {
enabled: true,
custom: null,
Expand Down Expand Up @@ -556,8 +564,7 @@ module.exports = function(Chart) {
}
}

var bgColor = helpers.color(vm.backgroundColor);
ctx.fillStyle = bgColor.alpha(opacity * bgColor.alpha()).rgbString();
ctx.fillStyle = mergeOpacity(vm.backgroundColor, opacity);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
Expand All @@ -575,8 +582,7 @@ module.exports = function(Chart) {
var titleFontSize = vm.titleFontSize,
titleSpacing = vm.titleSpacing;

var titleFontColor = helpers.color(vm.titleFontColor);
ctx.fillStyle = titleFontColor.alpha(opacity * titleFontColor.alpha()).rgbString();
ctx.fillStyle = mergeOpacity(vm.titleFontColor, opacity);
ctx.font = helpers.fontString(titleFontSize, vm._titleFontStyle, vm._titleFontFamily);

var i, len;
Expand All @@ -598,8 +604,7 @@ module.exports = function(Chart) {
ctx.textAlign = vm._bodyAlign;
ctx.textBaseline = 'top';

var bodyFontColor = helpers.color(vm.bodyFontColor);
var textColor = bodyFontColor.alpha(opacity * bodyFontColor.alpha()).rgbString();
var textColor = mergeOpacity(vm.bodyFontColor, opacity);
ctx.fillStyle = textColor;
ctx.font = helpers.fontString(bodyFontSize, vm._bodyFontStyle, vm._bodyFontFamily);

Expand All @@ -624,15 +629,15 @@ module.exports = function(Chart) {
// Draw Legend-like boxes if needed
if (drawColorBoxes) {
// Fill a white rect so that colours merge nicely if the opacity is < 1
ctx.fillStyle = helpers.color(vm.legendColorBackground).alpha(opacity).rgbaString();
ctx.fillStyle = mergeOpacity(vm.legendColorBackground, opacity);
ctx.fillRect(pt.x, pt.y, bodyFontSize, bodyFontSize);

// Border
ctx.strokeStyle = helpers.color(vm.labelColors[i].borderColor).alpha(opacity).rgbaString();
ctx.strokeStyle = mergeOpacity(vm.labelColors[i].borderColor, opacity);
ctx.strokeRect(pt.x, pt.y, bodyFontSize, bodyFontSize);

// Inner square
ctx.fillStyle = helpers.color(vm.labelColors[i].backgroundColor).alpha(opacity).rgbaString();
ctx.fillStyle = mergeOpacity(vm.labelColors[i].backgroundColor, opacity);
ctx.fillRect(pt.x + 1, pt.y + 1, bodyFontSize - 2, bodyFontSize - 2);

ctx.fillStyle = textColor;
Expand Down Expand Up @@ -660,8 +665,7 @@ module.exports = function(Chart) {
ctx.textAlign = vm._footerAlign;
ctx.textBaseline = 'top';

var footerFontColor = helpers.color(vm.footerFontColor);
ctx.fillStyle = footerFontColor.alpha(opacity * footerFontColor.alpha()).rgbString();
ctx.fillStyle = mergeOpacity(vm.footerFontColor, opacity);
ctx.font = helpers.fontString(vm.footerFontSize, vm._footerFontStyle, vm._footerFontFamily);

helpers.each(footer, function(line) {
Expand All @@ -671,8 +675,7 @@ module.exports = function(Chart) {
}
},
drawBackground: function(pt, vm, ctx, tooltipSize, opacity) {
var bgColor = helpers.color(vm.backgroundColor);
ctx.fillStyle = bgColor.alpha(opacity * bgColor.alpha()).rgbString();
ctx.fillStyle = mergeOpacity(vm.backgroundColor, opacity);
helpers.drawRoundedRectangle(ctx, pt.x, pt.y, tooltipSize.width, tooltipSize.height, vm.cornerRadius);
ctx.fill();
},
Expand Down

0 comments on commit abef7a2

Please sign in to comment.