--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2016
+ */
+
+/**
+ * Static utility functions for working with colors
+ */
+class CRM_Utils_Color {
+
+ /**
+ * Determine the appropriate text color for a given background.
+ *
+ * Based on YIQ value.
+ *
+ * @param string $hexcolor
+ * @return string
+ */
+ public static function getContrast($hexcolor) {
+ $hexcolor = trim($hexcolor, ' #');
+ $r = hexdec(substr($hexcolor, 0, 2));
+ $g = hexdec(substr($hexcolor, 2, 2));
+ $b = hexdec(substr($hexcolor, 4, 2));
+ $yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
+ return ($yiq >= 128) ? 'black' : 'white';
+ }
+
+}
--- /dev/null
+<?php
+
+/**
+ * Class CRM_Utils_ColorTest
+ * @group headless
+ */
+class CRM_Utils_ColorTest extends CiviUnitTestCase {
+
+ /**
+ * @dataProvider contrastExamples
+ */
+ public function testGetContrast($background, $text) {
+ $this->assertEquals($text, CRM_Utils_Color::getContrast($background));
+ }
+
+ public function contrastExamples() {
+ return array(
+ array('ef4444', 'white'),
+ array('FAA31B', 'black'),
+ array('FFF000', 'black'),
+ array(' 82c341', 'black'),
+ array('#009F75', 'white'),
+ array('#88C6eD', 'black'),
+ array('# 394ba0', 'white'),
+ array(' #D54799', 'white'),
+ );
+ }
+
+}