CRM-19769 - Add colorContrast js function
authorColeman Watts <coleman@civicrm.org>
Tue, 10 Jan 2017 04:19:44 +0000 (23:19 -0500)
committerColeman Watts <coleman@civicrm.org>
Tue, 10 Jan 2017 04:19:44 +0000 (23:19 -0500)
js/Common.js
templates/CRM/Tag/Form/Tag.tpl

index 57ae68ed0238f658e211c68c1a7fa3dd049d694e..86f21fb46e73ee6b77ea9e10c84b0f7a965ae16c 100644 (file)
@@ -1673,4 +1673,15 @@ if (!CRM.vars) CRM.vars = {};
   CRM.utils.formatDate = function(input, outputFormat) {
     return input ? $.datepicker.formatDate(outputFormat || CRM.config.dateInputFormat, CRM.utils.makeDate(input)) : '';
   };
+
+  // Used to set appropriate text color for a given background
+  CRM.utils.colorContrast = function (hexcolor) {
+    hexcolor = hexcolor.replace(/[ #]/g, '');
+    var r = parseInt(hexcolor.substr(0, 2), 16),
+     g = parseInt(hexcolor.substr(2, 2), 16),
+     b = parseInt(hexcolor.substr(4, 2), 16),
+     yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
+    return (yiq >= 128) ? 'black' : 'white';
+  };
+
 })(jQuery, _);
index 3ccbb5fdd7fa5aa066b66fee3bc0f4072e9a8e4b..817fdddba812f83811d0778860ac36ca7c5e740e 100644 (file)
@@ -51,7 +51,7 @@
       });
       $('input.crm-contact-tagset').each(function() {
         $.each($(this).select2('data'), function (i, tag) {
-          tags.push('<span class="crm-tag-item" title="' + (tag.description || '') + '"' + (tag.color ? 'style="color: #fff; background-color: ' + tag.color + ';"' : '') + '>' + tag.label + '</span>');
+          tags.push('<span class="crm-tag-item" title="' + (tag.description || '') + '"' + (tag.color ? 'style="color: ' + CRM.utils.colorContrast(tag.color) + '; background-color: ' + tag.color + ';"' : '') + '>' + tag.label + '</span>');
         });
       });
       // contact summary tabs and search forms both listen for this event