Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Component / Utility / Color.php
index aa296e94bece57f60a8366f42577822f7b5b0b0d..e3cb56bc92691be5ab6d7426bd439c52e265ceb9 100644 (file)
@@ -94,4 +94,28 @@ class Color {
     return '#' . str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
   }
 
+  /**
+   * Normalize the hex color length to 6 characters for comparison.
+   *
+   * @param string $hex
+   *   The hex color to normalize.
+   *
+   * @return string
+   *   The 6 character hex color.
+   */
+  public static function normalizeHexLength($hex) {
+    // Ignore '#' prefixes.
+    $hex = ltrim($hex, '#');
+
+    if (strlen($hex) === 3) {
+      $hex[5] = $hex[2];
+      $hex[4] = $hex[2];
+      $hex[3] = $hex[1];
+      $hex[2] = $hex[1];
+      $hex[1] = $hex[0];
+    }
+
+    return '#' . $hex;
+  }
+
 }