access.php
1 year ago
checks.php
1 year ago
colors.php
1 year ago
data-presets.php
1 year ago
date-time.php
1 year ago
debug.php
1 year ago
education.php
1 year ago
escape-sanitize.php
1 year ago
filesystem-media.php
1 year ago
form-fields.php
1 year ago
forms.php
1 year ago
list.php
1 year ago
payments.php
1 year ago
plugins.php
1 year ago
privacy.php
1 year ago
providers.php
1 year ago
unused.php
1 year ago
utilities.php
1 year ago
colors.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper functions to work with colors. |
| 4 | * |
| 5 | * @since 1.8.0 |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Detect if we should use a light or dark color based on the color given. |
| 10 | * |
| 11 | * @link https://docs.woocommerce.com/wc-apidocs/source-function-wc_light_or_dark.html#608-627 |
| 12 | * |
| 13 | * @since 1.2.5 |
| 14 | * |
| 15 | * @param mixed $color Color value. |
| 16 | * @param string $dark Dark color value (default: '#000000'). |
| 17 | * @param string $light Light color value (default: '#FFFFFF'). |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | function wpforms_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
| 22 | |
| 23 | $hex = str_replace( '#', '', $color ); |
| 24 | |
| 25 | $c_r = hexdec( substr( $hex, 0, 2 ) ); |
| 26 | $c_g = hexdec( substr( $hex, 2, 2 ) ); |
| 27 | $c_b = hexdec( substr( $hex, 4, 2 ) ); |
| 28 | |
| 29 | $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
| 30 | |
| 31 | return $brightness > 155 ? $dark : $light; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Convert hex color value to RGB. |
| 36 | * |
| 37 | * @since 1.7.9 |
| 38 | * @since 1.8.5 New param and return type were added. |
| 39 | * |
| 40 | * @param string $hex Color value in hex format. |
| 41 | * @param bool $as_string Whether to return the RGB value as a string or array. |
| 42 | * |
| 43 | * @return string|array Color value in RGB format. |
| 44 | */ |
| 45 | function wpforms_hex_to_rgb( $hex, $as_string = true ) { |
| 46 | |
| 47 | $hex = ltrim( $hex, '#' ); |
| 48 | |
| 49 | // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF". |
| 50 | $rgb_parts = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $hex ); |
| 51 | |
| 52 | $rgb = []; |
| 53 | $rgb['R'] = hexdec( $rgb_parts[0] . $rgb_parts[1] ); |
| 54 | $rgb['G'] = hexdec( $rgb_parts[2] . $rgb_parts[3] ); |
| 55 | $rgb['B'] = hexdec( $rgb_parts[4] . $rgb_parts[5] ); |
| 56 | |
| 57 | // Return the RGB value as a string. |
| 58 | if ( $as_string ) { |
| 59 | return sprintf( |
| 60 | '%1$d, %2$d, %3$d', |
| 61 | $rgb['R'], |
| 62 | $rgb['G'], |
| 63 | $rgb['B'] |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return $rgb; // This is an array. |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get a lighter color hex value. |
| 72 | * |
| 73 | * @since 1.8.5 |
| 74 | * |
| 75 | * @param string $color Color hex value. |
| 76 | * @param int $factor Factor to lighten the color. |
| 77 | * |
| 78 | * @return string Lighter color hex value. |
| 79 | */ |
| 80 | function wpforms_hex_lighter( $color, $factor = 30 ) { |
| 81 | |
| 82 | $base = wpforms_hex_to_rgb( $color, false ); |
| 83 | |
| 84 | // Leave if we can't get the RGB values. |
| 85 | if ( empty( $base ) || count( $base ) !== 3 ) { |
| 86 | return ''; |
| 87 | } |
| 88 | |
| 89 | $hex = '#'; |
| 90 | |
| 91 | foreach ( $base as $channel ) { |
| 92 | $amount = 255 - $channel; |
| 93 | $amount = $amount / 100; |
| 94 | $amount = round( floatval( $amount * $factor ) ); |
| 95 | $new_decimal = $channel + $amount; |
| 96 | |
| 97 | $new_hex_component = dechex( $new_decimal ); |
| 98 | |
| 99 | if ( strlen( $new_hex_component ) < 2 ) { |
| 100 | $new_hex_component = '0' . $new_hex_component; |
| 101 | } |
| 102 | |
| 103 | $hex .= $new_hex_component; |
| 104 | } |
| 105 | |
| 106 | return $hex; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get a darker color hex value. |
| 111 | * |
| 112 | * @since 1.8.5 |
| 113 | * |
| 114 | * @param string $color Color hex value. |
| 115 | * @param int $factor Factor to darken the color. |
| 116 | * |
| 117 | * @return string Darker color hex value. |
| 118 | */ |
| 119 | function wpforms_hex_darker( $color, $factor = 30 ) { |
| 120 | |
| 121 | $base = wpforms_hex_to_rgb( $color, false ); |
| 122 | |
| 123 | // Leave if we can't get the RGB values. |
| 124 | if ( empty( $base ) || count( $base ) !== 3 ) { |
| 125 | return ''; |
| 126 | } |
| 127 | |
| 128 | $hex = '#'; |
| 129 | |
| 130 | foreach ( $base as $channel ) { |
| 131 | $amount = $channel / 100; |
| 132 | $amount = round( floatval( $amount * $factor ) ); |
| 133 | $new_decimal = $channel - $amount; |
| 134 | |
| 135 | $new_hex_component = dechex( $new_decimal ); |
| 136 | |
| 137 | if ( strlen( $new_hex_component ) < 2 ) { |
| 138 | $new_hex_component = '0' . $new_hex_component; |
| 139 | } |
| 140 | |
| 141 | $hex .= $new_hex_component; |
| 142 | } |
| 143 | |
| 144 | return $hex; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Generate a contrasting color based on the given color. |
| 149 | * |
| 150 | * This function calculates a contrasting color to ensure readability based on the provided color. |
| 151 | * |
| 152 | * @since 1.8.5 |
| 153 | * |
| 154 | * @param string $color The original color value. Color hex value. |
| 155 | * @param int $light_factor The factor to lighten the color. |
| 156 | * @param int $dark_factor The factor to darken the color. |
| 157 | * |
| 158 | * @return string The contrasting color value. |
| 159 | */ |
| 160 | function wpforms_generate_contrasting_color( $color, $light_factor = 30, $dark_factor = 30 ) { |
| 161 | |
| 162 | $is_dark = wpforms_light_or_dark( $color, 'light', 'dark' ) === 'dark'; |
| 163 | |
| 164 | return $is_dark ? wpforms_hex_lighter( $color, $light_factor ) : wpforms_hex_darker( $color, $dark_factor ); |
| 165 | } |
| 166 |