+ return self::rgbToHex($rgb);
+ }
+
+ /**
+ * Convert named color (e.g. springgreen) to hex
+ *
+ * @param $colorName
+ * @return string|null
+ */
+ public static function nameToHex($colorName) {
+ if (strpos($colorName, '#') !== FALSE || strpos($colorName, '(') !== FALSE) {
+ return NULL;
+ }
+ if (empty(Civi::$statics[__CLASS__]['names'])) {
+ Civi::$statics[__CLASS__]['names'] = json_decode(file_get_contents(Civi::paths()->getPath(self::COLOR_FILE)), TRUE);
+ }
+ return Civi::$statics[__CLASS__]['names'][strtolower($colorName)] ?? NULL;
+ }
+
+ /**
+ * Converts rgb array to hex string
+ *
+ * @param int[] $rgb
+ * @return string
+ */
+ public static function rgbToHex($rgb) {
+ $ret = '#';
+ foreach ($rgb as $dec) {
+ $ret .= str_pad(dechex($dec), 2, '0', STR_PAD_LEFT);
+ }
+ return $ret;
+ }
+
+ /**
+ * Validate color input and convert it to standard hex notation
+ *
+ * @param string $color
+ * @return bool
+ */
+ public static function normalize(&$color) {
+ $rgb = self::getRgb($color);
+ if ($rgb) {
+ $color = self::rgbToHex($rgb);
+ return TRUE;
+ }
+ return FALSE;