Skip to content

Instantly share code, notes, and snippets.

@sebdesign
Last active September 28, 2024 14:50
Show Gist options
  • Select an option

  • Save sebdesign/a65cc39e3bcd81201609e6a8087a83b3 to your computer and use it in GitHub Desktop.

Select an option

Save sebdesign/a65cc39e3bcd81201609e6a8087a83b3 to your computer and use it in GitHub Desktop.

Revisions

  1. sebdesign revised this gist Nov 8, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Color.php
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ public static function rgb(string $hex): array
    * @param int $b
    * @return float
    */
    public static function luminance (int $r, int $g, int $b): float
    public static function luminance(int $r, int $g, int $b): float
    {
    return 0.2126 * pow($r/255, 2.2) +
    0.7152 * pow($g/255, 2.2) +
    @@ -35,7 +35,7 @@ public static function luminance (int $r, int $g, int $b): float
    * @param string $C2 hex color
    * @return float
    */
    public static function relativeLuminance (string $C1, string $C2): float
    public static function relativeLuminance(string $C1, string $C2): float
    {
    $L1 = self::luminance(...self::rgb($C1));
    $L2 = self::luminance(...self::rgb($C2));
  2. sebdesign revised this gist Nov 8, 2021. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions Color.php
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,10 @@ class Color
    /**
    * Convert a hex color to RGB.
    *
    * @param string $hex #BADA55
    * @return array [186, 218, 85]
    * @param string $hex #BADA55
    * @return array{int,int,int} [186, 218, 85]
    */
    public static function rgb ($hex)
    public static function rgb(string $hex): array
    {
    return sscanf($hex, "#%02x%02x%02x");
    }
    @@ -21,7 +21,8 @@ public static function rgb ($hex)
    * @param int $b
    * @return float
    */
    public static function luminance ($r, $g, $b) {
    public static function luminance (int $r, int $g, int $b): float
    {
    return 0.2126 * pow($r/255, 2.2) +
    0.7152 * pow($g/255, 2.2) +
    0.0722 * pow($b/255, 2.2);
    @@ -34,7 +35,8 @@ public static function luminance ($r, $g, $b) {
    * @param string $C2 hex color
    * @return float
    */
    public static function relativeLuminance ($C1, $C2) {
    public static function relativeLuminance (string $C1, string $C2): float
    {
    $L1 = self::luminance(...self::rgb($C1));
    $L2 = self::luminance(...self::rgb($C2));

  3. sebdesign created this gist Jul 11, 2016.
    47 changes: 47 additions & 0 deletions Color.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php

    class Color
    {
    /**
    * Convert a hex color to RGB.
    *
    * @param string $hex #BADA55
    * @return array [186, 218, 85]
    */
    public static function rgb ($hex)
    {
    return sscanf($hex, "#%02x%02x%02x");
    }

    /**
    * Calculate the relative luminance of an RGB color.
    *
    * @param int $r
    * @param int $g
    * @param int $b
    * @return float
    */
    public static function luminance ($r, $g, $b) {
    return 0.2126 * pow($r/255, 2.2) +
    0.7152 * pow($g/255, 2.2) +
    0.0722 * pow($b/255, 2.2);
    }

    /**
    * Calculate the relative luminance of two colors.
    *
    * @param string $C1 hex color
    * @param string $C2 hex color
    * @return float
    */
    public static function relativeLuminance ($C1, $C2) {
    $L1 = self::luminance(...self::rgb($C1));
    $L2 = self::luminance(...self::rgb($C2));

    if ($L1 > $L2) {
    return ($L1 + 0.05) / ($L2 + 0.05);
    } else {
    return ($L2 + 0.05) / ($L1 + 0.05);
    }
    }
    }