CRM-19769 - Add color contrast utility function
authorColeman Watts <coleman@civicrm.org>
Thu, 15 Dec 2016 04:43:33 +0000 (23:43 -0500)
committerColeman Watts <coleman@civicrm.org>
Fri, 23 Dec 2016 22:06:19 +0000 (17:06 -0500)
CRM/Utils/Color.php [new file with mode: 0644]
tests/phpunit/CRM/Utils/ColorTest.php [new file with mode: 0644]

diff --git a/CRM/Utils/Color.php b/CRM/Utils/Color.php
new file mode 100644 (file)
index 0000000..6349f8b
--- /dev/null
@@ -0,0 +1,56 @@
+<?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';
+  }
+
+}
diff --git a/tests/phpunit/CRM/Utils/ColorTest.php b/tests/phpunit/CRM/Utils/ColorTest.php
new file mode 100644 (file)
index 0000000..77b894f
--- /dev/null
@@ -0,0 +1,29 @@
+<?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'),
+    );
+  }
+
+}