'#1f5bff', 'hover' => '#4565c4', 'background' => '#f6f7fb', 'border_1' => '#1f5bff', 'border_2' => '#f36fff', ); } function velischat_color_styles(){ // Ready-made styles: every color of the chatbot in one click. Each was checked for readable text and a main color // that stands out on its surfaces. 'surface' is the color of header, answers and fields: light (white) or dark. // Add-ons can add their own styles return (array) apply_filters( 'velischat_color_styles', array( 'default' => array( 'name' => __( 'Default', 'velischat' ), 'principal' => '#1f5bff', 'background' => '#f6f7fb', 'surface' => 'light', 'border_style' => 'colorful' ), 'light' => array( 'name' => _x( 'Light', 'color scheme', 'velischat' ), 'principal' => '#1f2937', 'background' => '#ffffff', 'surface' => 'light', 'border_style' => 'brand' ), 'dark' => array( 'name' => _x( 'Dark', 'color scheme', 'velischat' ), 'principal' => '#5b8cff', 'background' => '#0e1015', 'surface' => 'dark', 'border_style' => 'brand' ), ) ); } function velischat_get_color_style(){ // The style chosen in the settings, Default when it isn't one of the available styles $style = get_option( 'velischat_color_style', '' ); $style = isset( velischat_color_styles()[ $style ] ) ? $style : 'default'; return (string) apply_filters( 'velischat_color_style', $style ); } function velischat_sanitize_color_style( $value ){ // One of the ready-made styles $style = isset( velischat_color_styles()[ $value ] ) ? $value : 'default'; return (string) apply_filters( 'velischat_sanitize_color_style', $style, $value ); } function velischat_get_principal_color( $style ){ // The main color: the one chosen in the settings, or the style's own when none was chosen $color = sanitize_hex_color( get_option( 'velischat_color_1', '' ) ); return $color ? strtolower( $color ) : $style[ 'principal' ]; } function velischat_get_colors(){ // Every color the chatbot uses: those of the chosen style, with the main color chosen in the settings. Add-ons can // change them $styles = velischat_color_styles(); $style = velischat_style_colors( isset( $styles[ velischat_get_color_style() ] ) ? velischat_get_color_style() : 'default' ); $style[ 'principal' ] = velischat_get_principal_color( $style ); if( $style[ 'border_style' ] === 'brand' ){ // The border made from the main color follows it $style[ 'border_1' ] = $style[ 'principal' ]; $style[ 'border_2' ] = velischat_brand_border_color( $style[ 'principal' ] ); } return (array) apply_filters( 'velischat_colors', velischat_complete_style_colors( $style ) ); } function velischat_hex_to_rgb( $hex ){ // Red, green and blue of a #rrggbb or #rgb color, 0-255, or null when it isn't one $hex = ltrim( (string) $hex, '#' ); if( strlen( $hex ) === 3 ){ $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } if( ! preg_match( '/^[0-9a-f]{6}$/i', $hex ) ){ return null; } return array( hexdec( substr( $hex, 0, 2 ) ), hexdec( substr( $hex, 2, 2 ) ), hexdec( substr( $hex, 4, 2 ) ) ); } function velischat_color_luminance( $hex ){ // Relative luminance of a color (WCAG), from 0 (black) to 1 (white) $rgb = velischat_hex_to_rgb( $hex ); if( ! $rgb ){ return 1; } $luminance = 0; foreach( array( 0.2126, 0.7152, 0.0722 ) as $i => $weight ){ $channel = $rgb[ $i ] / 255; $channel = $channel <= 0.03928 ? $channel / 12.92 : pow( ( $channel + 0.055 ) / 1.055, 2.4 ); $luminance += $weight * $channel; } return $luminance; } function velischat_color_contrast( $a, $b ){ // Contrast ratio between two colors (WCAG), from 1 to 21 $la = velischat_color_luminance( $a ); $lb = velischat_color_luminance( $b ); return ( max( $la, $lb ) + 0.05 ) / ( min( $la, $lb ) + 0.05 ); } function velischat_mix_colors( $a, $b, $weight ){ // $weight (0-1) of color $a mixed with the rest of color $b, as #rrggbb $ra = velischat_hex_to_rgb( $a ); $rb = velischat_hex_to_rgb( $b ); if( ! $ra || ! $rb ){ return $a; } $out = '#'; for( $i = 0; $i < 3; $i++ ){ $out .= sprintf( '%02x', (int) round( $ra[ $i ] * $weight + $rb[ $i ] * ( 1 - $weight ) ) ); } return $out; } function velischat_is_dark_color( $hex ){ // Whether white text reads better than the chatbot's dark text on this color return velischat_color_contrast( $hex, '#ffffff' ) >= velischat_color_contrast( $hex, VELISCHAT_TEXT_DARK ); } function velischat_get_text_color_on( $hex ){ // White or dark text, whichever is more readable on the given background return velischat_is_dark_color( $hex ) ? '#ffffff' : VELISCHAT_TEXT_DARK; } function velischat_auto_hover_color( $principal ){ // Color of the buttons under the mouse: the main color a little darker, or lighter when it's already very dark return velischat_color_luminance( $principal ) > 0.04 ? velischat_mix_colors( $principal, '#000000', 0.82 ) : velischat_mix_colors( $principal, '#ffffff', 0.78 ); } function velischat_background_text_vars( $background ){ // Texts written straight on the chat background (greeting, suggestions title, notes): white on a dark background, // darker greys on a colored one where the usual greys would be too faint. Nothing on the usual light backgrounds, // so the stylesheet keeps its own greys if( velischat_is_dark_color( $background ) ){ return array( '--velis-on-bg' => '#ffffff', '--velis-on-bg-muted' => 'rgba(255,255,255,0.74)' ); } if( velischat_color_contrast( $background, '#6b7080' ) < 4.5 ){ return array( '--velis-on-bg' => VELISCHAT_TEXT_DARK, '--velis-on-bg-muted' => '#33373f' ); } return array(); } function velischat_brand_border_color( $principal ){ // Second color of the main color's border: a light tint of it return velischat_mix_colors( $principal, '#ffffff', 0.4 ); } function velischat_surface_colors( $surface, $background ){ // Colors of header, answers, fields and cards. Light surfaces return nothing: the stylesheet keeps its own whites // and greys. Dark surfaces are tints of the background when it's dark, otherwise of a neutral dark grey if( $surface !== 'dark' ){ return array(); } $base = velischat_is_dark_color( $background ) ? $background : '#16181d'; return array( 'surface' => velischat_mix_colors( '#ffffff', $base, 0.07 ), 'surface-alt' => velischat_mix_colors( '#ffffff', $base, 0.11 ), 'surface-muted' => velischat_mix_colors( '#ffffff', $base, 0.13 ), 'surface-hover' => velischat_mix_colors( '#ffffff', $base, 0.19 ), 'line' => velischat_mix_colors( '#ffffff', $base, 0.15 ), 'line-strong' => velischat_mix_colors( '#ffffff', $base, 0.26 ), 'text' => '#eef0f4', 'text-muted' => 'rgba(255,255,255,0.7)', 'text-faint' => 'rgba(255,255,255,0.5)', 'danger' => '#ff8a80', 'success' => '#5ad48d', 'color-scheme' => 'dark', ); } function velischat_surface_color( $surface, $background ){ // The color of answers and header, to check that the main color stands out on it $colors = velischat_surface_colors( $surface, $background ); return $colors ? $colors[ 'surface' ] : '#ffffff'; } function velischat_style_colors( $key ){ // Every color of a ready-made style, in the shape of velischat_get_colors() $styles = velischat_color_styles(); if( ! isset( $styles[ $key ] ) ){ return null; } $style = $styles[ $key ]; if( $style[ 'border_style' ] === 'brand' ){ $style[ 'border_1' ] = $style[ 'principal' ]; $style[ 'border_2' ] = velischat_brand_border_color( $style[ 'principal' ] ); } elseif( $style[ 'border_style' ] === 'colorful' ){ $style[ 'border_1' ] = velischat_color_defaults()[ 'border_1' ]; $style[ 'border_2' ] = velischat_color_defaults()[ 'border_2' ]; } return $style; } function velischat_complete_style_colors( $style ){ // A ready-made style in the full shape of velischat_get_colors(): text colors and the automatic mouse-over color $hover = velischat_auto_hover_color( $style[ 'principal' ] ); return array( 'principal' => $style[ 'principal' ], 'on_principal' => velischat_get_text_color_on( $style[ 'principal' ] ), 'hover_mode' => 'auto', 'hover' => $hover, 'on_hover' => velischat_get_text_color_on( $hover ), 'background' => $style[ 'background' ], 'surface' => $style[ 'surface' ], 'border_style' => $style[ 'border_style' ], 'border_1' => $style[ 'border_1' ], 'border_2' => $style[ 'border_2' ], ); } function velischat_output_color_vars() { // Plugin colors, font and launcher position as CSS custom properties in the frontend if( ! velischat_can_use_chatbot() ){ return; } $colors = velischat_get_colors(); $vars = array( '--velis-color-1' => $colors[ 'principal' ], '--velis-on-color-1' => $colors[ 'on_principal' ], '--velis-color-2' => $colors[ 'hover' ], '--velis-on-color-2' => $colors[ 'on_hover' ], '--velis-bg' => $colors[ 'background' ], '--velis-border-1' => $colors[ 'border_1' ], '--velis-border-2' => $colors[ 'border_2' ], ) + velischat_background_text_vars( $colors[ 'background' ] ); foreach( velischat_surface_colors( $colors[ 'surface' ], $colors[ 'background' ] ) as $name => $value ){ $vars[ '--velis-' . $name ] = $value; } // Custom distances of the compact icon from the page edges; when empty the stylesheet defaults apply foreach( array( 'bottom', 'right' ) as $side ){ $offset = get_option( 'velischat_compact_offset_' . $side, '' ); if( $offset !== '' ){ $vars[ '--velis-launcher-' . $side ] = absint( $offset ) . 'px'; } } $css = ''; foreach( $vars as $name => $value ){ $css .= $name . ':' . esc_attr( $value ) . ';'; } // The font family is a fixed value from velischat_get_font_config(), and the avatar an escaped URL $css .= '--velis-font:' . velischat_get_font_config( get_option( 'velischat_font', 'fira_sans' ) )[ 'family' ] . ';'; $css .= '--velis-agent-avatar:url("' . esc_url( velischat_get_floating_image() ) . '");'; wp_add_inline_style( 'velischat-style', ':root{' . $css . '}' ); } // After velischat_enqueue_assets(), which adds the stylesheet add_action( 'wp_enqueue_scripts', 'velischat_output_color_vars', 20 );