= self::contrast( $bg, '#000000' ) ? '#ffffff' : '#000000'; } /** * Accent variant usable as text/focus colour on white (≥ 4.5:1), darkening if needed. * * @param string $hex Accent. * @return string */ public static function ink_on_white( $hex ) { $hex = sanitize_hex_color( $hex ); if ( ! $hex ) { return '#111111'; } $hex = ltrim( $hex, '#' ); if ( 3 === strlen( $hex ) ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } $r = hexdec( substr( $hex, 0, 2 ) ); $g = hexdec( substr( $hex, 2, 2 ) ); $b = hexdec( substr( $hex, 4, 2 ) ); for ( $i = 0; $i < 20; $i++ ) { $color = sprintf( '#%02x%02x%02x', $r, $g, $b ); if ( self::contrast( $color, '#ffffff' ) >= 4.5 ) { return $color; } $r = (int) floor( $r * 0.85 ); $g = (int) floor( $g * 0.85 ); $b = (int) floor( $b * 0.85 ); } return '#111111'; } /** * Sanitise a CSV cell against spreadsheet formula injection. * * @param mixed $value Value. * @return string */ public static function csv_cell( $value ) { $value = (string) $value; if ( '' !== $value && in_array( $value[0], array( '=', '+', '-', '@', "\t", "\r" ), true ) ) { $value = "'" . $value; } return $value; } }