+ return ($yiq >= 128) ? $black : $white;
+ }
+
+ /**
+ * Convert hex color to decimal
+ *
+ * @param string $hexcolor
+ * @return array
+ * [red, green, blue]
+ */
+ public static function getRgb($hexcolor) {
+ $hexcolor = trim($hexcolor, ' #');
+ if (strlen($hexcolor) === 3) {
+ $hexcolor = $hexcolor[0] . $hexcolor[0] . $hexcolor[1] . $hexcolor[1] . $hexcolor[2] . $hexcolor[2];
+ }
+ return [
+ hexdec(substr($hexcolor, 0, 2)),
+ hexdec(substr($hexcolor, 2, 2)),
+ hexdec(substr($hexcolor, 4, 2)),
+ ];
+ }
+
+ /**
+ * Calculate a highlight color from a base color
+ *
+ * @param $hexcolor
+ * @return string
+ */
+ public static function getHighlight($hexcolor) {
+ $rgb = CRM_Utils_Color::getRgb($hexcolor);
+ $avg = array_sum($rgb) / 3;
+ foreach ($rgb as &$v) {
+ if ($avg > 242) {
+ // For very bright values, lower the brightness
+ $v -= 50;
+ }
+ else {
+ // Bump up brightness on a nonlinear curve - darker colors get more of a boost
+ $v = min(255, intval((-.0035 * ($v - 242) ** 2) + 260));
+ }
+ }
+ return '#' . implode(array_map('dechex', $rgb));