<?php

/**
 * @version    1.1
 * @package    Sparta
 * @author     Univer Team <support@univertheme.com>
 * @copyright  Copyright (C) 2014 Univertheme.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 */
class Sparta_Assets
{
    /**
     * Variable to hold the initialization state.
     *
     * @var  boolean
     */
    protected static $initialized = false;

    /**
     * Initialize functions.
     *
     * @return  void
     */
    public static function initialize()
    {
        // Do nothing if start functions already initialized.
        if (self::$initialized) {
            return;
        }
        // Register actions to do something.
        add_action('wp_enqueue_scripts', array(__CLASS__, 'sparta_scripts_loader'));
        add_action('wp_enqueue_scripts', array(__CLASS__, 'print_embed_style'), 9999);
        add_action('admin_enqueue_scripts', array(__CLASS__, 'sparta_admin_enqueue_scripts'), 5);

        // State that initialization completed.
        self::$initialized = true;
    }

    /**
     * Print Styles in to Head
     */
    public static function print_embed_style()
    {
        $style = self::get_embed_style();

        $style = preg_replace('#/\*.*?\*/#s', '', $style);
        $style = preg_replace('/\s*([{}|:;,])\s+/', '$1', $style);
        $style = preg_replace('/\s\s+(.*)/', '$1', $style);

        wp_register_style('z_sparta_front_style', false);
        wp_enqueue_style('z_sparta_front_style');
        wp_add_inline_style('z_sparta_front_style', $style);
    }

    /**
     * Enqueue scripts and styles.
     */
    public static function sparta_scripts_loader()
    {
        wp_enqueue_style('font-awesome', self::get_assets_url() . '/fonts/font-awesome/css/font-awesome.min.css');
        wp_enqueue_style('font-themify', self::get_assets_url() . '/fonts/themify/themify-icons.css');
        wp_enqueue_style('bootstrap', self::get_assets_url() . '/vendors/bootstrap/css/bootstrap.min.css');
        wp_enqueue_style('lightbox', self::get_assets_url() . '/vendors/lightbox/css/lightbox.min.css');
        wp_enqueue_style('animate', get_parent_theme_file_uri() . '/assets/css/animate.css', array(), null);
        wp_enqueue_style('slick', self::get_assets_url() . '/vendors/slick/slick.css');
        wp_enqueue_style('sparta-tether', self::get_assets_url() . '/vendors/tether/css/tether.min.css');
        wp_enqueue_style('magnific-popup', get_parent_theme_file_uri() . '/assets/vendors/magnific-popup/magnific-popup.css');
        wp_enqueue_style('lightgallery', self::get_assets_url() . '/vendors/lightgallery/css/lightgallery.min.css');

        wp_enqueue_style('sparta-style', get_stylesheet_uri());
        wp_enqueue_style('sparta-supper', get_parent_theme_file_uri() . '/assets/css/sparta.css', array(), null);
        wp_enqueue_style('sparta-menu', get_parent_theme_file_uri() . '/assets/css/menu.css', array(), null);


        wp_enqueue_script('jquery-ui-slider');
        wp_enqueue_script('masonry');
        wp_enqueue_script('modernizr', get_parent_theme_file_uri() . '/assets/js/modernizr.js', array(), Sparta_Core::VERSION, true);
        wp_enqueue_script('bootstrap', get_parent_theme_file_uri() . '/assets/vendors/bootstrap/js/bootstrap.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('isotope', get_template_directory_uri() . '/assets/vendors/isotope.pkgd.min.js', array('jquery'), true);
        wp_enqueue_script('page-scroll-to-id', get_template_directory_uri() . '/assets/vendors/page-scroll-to-id.min.js', array('jquery'), true);
        wp_enqueue_script('slicknav', get_template_directory_uri() . '/assets/vendors/slicknav/jquery.slicknav.js', array('jquery'), true);
        wp_enqueue_script('animateNumber', get_template_directory_uri() . '/assets/vendors/jquery.animateNumber.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('lightbox', get_template_directory_uri() . '/assets/vendors/lightbox/js/lightbox.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('magnific-popup', get_parent_theme_file_uri() . '/assets/vendors/magnific-popup/jquery-magnific-popup.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('slick', get_parent_theme_file_uri() . '/assets/vendors/slick/slick.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('countdown', get_parent_theme_file_uri() . '/assets/vendors/jquery.countdown.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('lightgallery', get_parent_theme_file_uri() . '/assets/vendors/lightgallery/js/lightgallery.min.js', array('jquery'), Sparta_Core::VERSION, true);
        wp_enqueue_script('sparta-main', get_parent_theme_file_uri() . '/assets/js/main.js', array('jquery'), Sparta_Core::VERSION, true);

        wp_localize_script('sparta-main', 'screenReaderText', array(
            'expand' => esc_html__('expand child menu', 'sparta'),
            'collapse' => esc_html__('collapse child menu', 'sparta'),
        ));

        if (is_singular() && comments_open() && get_option('thread_comments')) {
            wp_enqueue_script('comment-reply');
        }

        $customizer_heading_font = get_theme_mod('sparta_heading_content_font' ,[]);
        $customizer_body_font = get_theme_mod('sparta_body_typography_font_family',[]);

        $protocol = is_ssl() ? 'https' : 'http';

        if (empty($customizer_body_font)) {
            $sparta_df_body_fonts = add_query_arg(
                array(
                    'family' => urldecode('"Source Sans Pro":300,300i,400,400i,600,600i,700,700i,900,900i'),
                    'subset' => urlencode('latin,latin-ext'),
                ),
                "{$protocol}://fonts.googleapis.com/css"
            );
            wp_enqueue_style('body-google-fonts', esc_url($sparta_df_body_fonts), false);
        }


        if (empty($customizer_heading_font)) {
            $sparta_df_heading_fonts = add_query_arg(
                array(
                    'family' => urldecode('"Playfair Display":400,400i,700,700i,900,900i'),
                    'subset' => urlencode('latin,latin-ext'),
                ),
                "{$protocol}://fonts.googleapis.com/css"
            );

            wp_enqueue_style('heading-google-fonts', esc_url($sparta_df_heading_fonts), false);
        }

        if (!class_exists('Univer_Core')) {
            wp_enqueue_style('sparta-default', get_parent_theme_file_uri() . '/assets/css/default.css', array(), null);
        }
    }

    /**
     * Get options from Customizer and mix to CSS
     */

    public static function get_embed_style()
    {
        global $post, $woocommerce;
        $page_id = get_the_ID();

        $page_meta_hidden_footer = $page_meta_bg_img = $page_meta_bg_color = $meta_bg_footer_image_url = $custom_bg_page_image_url = $meta_bgimg_page_title_url = $page_header_trans = '';
        if ($page_id) {
            $meta_bgimg_page_title_url = get_post_meta($page_id, 'univer_page_title_bg_img', true);
            $page_header_trans = get_post_meta($page_id, 'custom_page_header_trans', true);

            $custom_bg_page_image = get_post_meta($page_id, 'custom_bg_page_image', true);

            if ($custom_bg_page_image) {
                $custom_bg_page_image_url = wp_get_attachment_image_url($custom_bg_page_image, 'full');
            }

            $meta_bg_footer_image_url = get_post_meta($page_id, 'univer_footer_bg_img', true);
            $page_meta_bg_color = esc_html(get_post_meta($page_id, 'univer_page_bg_color', true));
            $page_meta_bg_img = esc_html(get_post_meta($page_id, 'univer_page_bg_img', true));
            $page_meta_hidden_footer = get_post_meta($page_id, 'univer_hidden_footer', true);
            $meta_header_style = get_post_meta($page_id, 'univer_header_style', true);
            $page_meta_header_bg_color = get_post_meta($page_id, 'univer_header_bg_color', true);
            $page_meta_header_txt_color = get_post_meta($page_id, 'univer_header_txt_color', true);
        }

        $sparta_button_left_right = get_theme_mod('sparta_button_left_right');
        $sparta_button_top_bottom = get_theme_mod('sparta_button_top_bottom');
        $sparta_pages_404_bg_img = get_theme_mod('sparta_pages_404_bg_img');
        $sparta_heading_content_font = get_theme_mod('sparta_heading_content_font');
        $sparta_body_typography = get_theme_mod('sparta_body_typography');
        $sparta_heading_content_font_2 = get_theme_mod('sparta_heading_content_font_2');
        $sparta_body_font_type = explode(",", get_theme_mod('sparta_body_font_type', ''));
        $sparta_heading_font_type = explode(",", get_theme_mod('sparta_heading_font_type', ''));
        $sparta_heading_font_type_2 = explode(",", get_theme_mod('sparta_heading_font_type_2', ''));
        $sparta_css_animation_type = get_theme_mod('sparta_css_animation_type', '1');
        $sparta_css_icon_color = get_theme_mod('sparta_css_icon_color', '#b0914f');

        $sparta_color_pagetitle_bg_img = get_theme_mod('sparta_color_pagetitle_bg_img', '');
        $hotel_pagetitle_bg_img = get_theme_mod('sparta_hotel_pagetitle_bg_img', '');
        $events_pagetitle_bg_img = get_theme_mod('sparta_events_pagetitle_bg_img', '');

        $sparta_event_list_head_img = get_theme_mod('sparta_event_list_head_img', '');
        $sparta_color_footer_bg_img = get_theme_mod('sparta_color_footer_bg_img', '');
        $woo_headerbg_img = get_theme_mod('sparta_woo_general_headerbg_img', '');
        $sparta_color_primary = get_theme_mod('sparta_color_primary', '#b0914f');
        $color_primary_rgb = self::sparta_hexToRgb($sparta_color_primary);

        $css = "";

        if ($sparta_body_font_type[0] === 'custom') {
            $body_custom_font_url = array_slice($sparta_body_font_type, 1, -1);
            $css .= "
            @font-face {
                font-family: '" . end($sparta_body_font_type) . "';            
            ";
            foreach ($body_custom_font_url as $url) {
                $css .= " src: url('" . $url . "');";
            }
            $css .= "
            }
            ";
            $css .= '
                body {font-family: "' . end($sparta_body_font_type) . '" ; } 
            ';
        }

        if (isset($sparta_body_typography['font-weight']) && $sparta_body_typography['font-weight']) {
            $css .= 'body {font-weight:' . $sparta_body_typography["font-weight"] . ';  }';
        }
        if (isset($sparta_heading_content_font['font-weight']) && $sparta_heading_content_font['font-weight'] !== '') {
            $css .= '
                h1,h2,.widget-lst-title,#reply-title, .relatedposts-title, .sidebar-title, .font-h1,.main-footer .widget_nav_menu ul li a { font-weight:' . $sparta_heading_content_font["font-weight"] . '; }
            ';
        }
        if (isset($sparta_heading_content_font_2['font-weight']) && $sparta_heading_content_font_2['font-weight'] !== '') {
            $css .= '
                h3,h4,h5,h6 { font-weight:' . $sparta_heading_content_font_2["font-weight"] . '; }
            ';
        }

        if ($sparta_heading_font_type[0] === 'custom') {
            $h1_custom_font_url = array_slice($sparta_heading_font_type, 1, -1);

            $css .= "
            @font-face {
                font-family: '" . end($sparta_heading_font_type) . "';            
            ";

            foreach ($h1_custom_font_url as $url) {
                $css .= "src: url('" . $url . "');";
            }

            $css .= "
            }
            ";
            $css .= '
                h1,h2,.widget-lst-title,#reply-title, .relatedposts-title,.sparta-event-meta .left-meta-event,.sparta-date-event, .sidebar-title, .font-h1,.main-footer .widget_nav_menu ul li a,.dropcap {font-family: "' . end($sparta_heading_font_type) . '" ; } 
            ';
        }

        if ($sparta_heading_font_type_2[0] === 'custom') {
            $h3_custom_font_url = array_slice($sparta_heading_font_type_2, 1, -1);
            $css .= "
            @font-face {
                font-family: '" . end($sparta_heading_font_type_2) . "';            
            ";

            foreach ($h3_custom_font_url as $url) {
                $css .= "src: url('" . $url . "');";
            }
            $css .= "
            }
            ";
            $css .= '
                h3,h4,h5,h6 {font-family: "' . end($sparta_heading_font_type_2) . '" ; } 
            ';
        }
        if ($page_meta_bg_color && $page_meta_bg_color != '' && !is_search()) {
            $css .= '.page article.page, .vk-page, .vk-content {background-color: ' . $page_meta_bg_color . '; }';
        }
        if ($page_meta_bg_img && $page_meta_bg_img != '') {
            $css .= '.vk-content {background-image:url("' . $page_meta_bg_img . '"); background-size: cover; }';
            if (is_page_template('temp-comming-soon.php')) :
                $css .= '.vk-coming-soon {background-image:url("' . $page_meta_bg_img . '"); background-size: cover; }';
            endif;
        }
        if ($page_meta_hidden_footer && $page_meta_hidden_footer == 'yes') {
            $css .= '.vk-footer {display:none; }';
        }

        if ($meta_bg_footer_image_url != '') {
            $css .= '
                .vk-footer {background-image: url(' . esc_url($meta_bg_footer_image_url) . '); background-size: cover;} 
            ';
            if (true == get_theme_mod('sparta_color_footer_bg_parallax', false)) {
                $css .= '
                    .vk-footer {background-attachment: fixed;background-position: center;} 
                ';
            }
        } elseif ($sparta_color_footer_bg_img) {
            $css .= '
                .vk-footer {background-image: url(' . esc_url($sparta_color_footer_bg_img) . '); background-size: cover;} 
            ';
            if (true == get_theme_mod('sparta_color_footer_bg_parallax', false)) {
                $css .= '
                    @media (min-width: 992px){.vk-footer {background-attachment: fixed;background-position: center;} }
                ';
            }
        }

        if ($sparta_event_list_head_img) {
            $css .= '
                .sparta-page-title.events-pagetitle {background-image: url(' . esc_url($sparta_event_list_head_img) . '); } 
            ';
            if (get_post_type() == 'espresso_events') {
                if (true == get_theme_mod('sparta_event_header_trans', false)) :
                    $css .= '
                       .sparta-page-title.events-pagetitle {padding-top:130px; } 
                        ';
                endif;
            }
        }
        if ($page_header_trans != '') {
            $css .= '.sparta-page-title {padding-top:130px; }';
        }

        if (is_page() && $meta_header_style == 'fullwidth' && $page_meta_header_bg_color !== '' && $page_meta_header_txt_color !== '') {
            $css .= '
                .vk-header.header-fullwidth  { 
                background-color:' . $page_meta_header_bg_color . '; 
                color: ' . $page_meta_header_txt_color . ';
                }
                
                .box_sticks .stick{
                    background-color:' . $page_meta_header_txt_color . '; 
                }
                .vk-header-fixed.fixed-header .vk-btn-navbar-toggle .ti-menu, .vk-header-fixed.fixed-header .vk-btn-navbar-toggle .ti-close{
                color: ' . $page_meta_header_txt_color . ';
                }
                @media screen and (min-width: 992px){
                    .vk-header.header-fullwidth a span{
                        color: ' . $page_meta_header_txt_color . ';
                    }
                    .header-fullwidth ul.vk-nav ul.sub-menu {
                        background-color:' . $page_meta_header_bg_color . '; 
                    }
                    ul.vk-nav ul.sub-menu li .icon-has-children, .header-fullwidth ul.vk-nav ul.sub-menu li a{
                        color: ' . $page_meta_header_txt_color . ';
                    }
                }
                
            ';
        }

        if ($meta_bgimg_page_title_url != '') {
            $css .= '
                .page-title-bg {background-image: url(' . esc_url($meta_bgimg_page_title_url) . '); } 
            ';
        } else {
            if (get_post_type() === 'espresso_events' && $events_pagetitle_bg_img !== '') {

                $css .= '
                .page-title-bg{background-image: url(' . esc_url($events_pagetitle_bg_img) . '); } 
                ';
            } elseif (get_post_type() === 'mphb_room_type' && $hotel_pagetitle_bg_img !== '') {

                $css .= '
                .page-title-bg{background-image: url(' . esc_url($hotel_pagetitle_bg_img) . '); } 
                ';
            } elseif ($woo_headerbg_img != '' && ($woocommerce && is_shop() || $woocommerce && is_product() || $woocommerce && is_product_category() || $woocommerce && is_product_tag()
                    || $woocommerce && is_account_page() || $woocommerce && is_cart() || $woocommerce && is_checkout())
            ) {
                $css .= '
                .page-title-bg.page-title-product,
                    .page-title-product {background-image: url(' . esc_url($woo_headerbg_img) . '); } 
                ';
            } elseif ($sparta_color_pagetitle_bg_img && get_post_type() != 'espresso_events') {
                $css .= '
                    .page-title-bg {background-image: url(' . esc_url($sparta_color_pagetitle_bg_img) . '); } 
                ';
            }
        }

        if ($custom_bg_page_image_url != '') {
            $css .= '
                #page.site {background-image: url(' . esc_url($custom_bg_page_image_url) . '); background-size: cover;} 
            ';
        }

        if ($sparta_button_left_right) {
            $css .= ' .widget_shopping_cart_content .buttons a,.btn,input[type="submit"],.woocommerce #respond input#submit.alt, .woocommerce a.button.alt, .woocommerce button.button.alt, .woocommerce input.button.alt,
            .woocommerce #respond input#submit, .woocommerce a.button, .woocommerce button.button, .woocommerce input.button{ padding-right:' . $sparta_button_left_right . 'px; padding-left:' . $sparta_button_left_right . 'px;}
            ';
        }
        if ($sparta_button_top_bottom) {
            $css .= '.widget_shopping_cart_content .buttons a,.btn,input[type="submit"],.woocommerce #respond input#submit.alt, .woocommerce a.button.alt, .woocommerce button.button.alt, .woocommerce input.button.alt,
            .woocommerce #respond input#submit, .woocommerce a.button, .woocommerce button.button, .woocommerce input.button{ padding-top:' . $sparta_button_top_bottom . 'px; padding-bottom:' . $sparta_button_top_bottom . 'px;}
            ';
        }
        if ($sparta_pages_404_bg_img) {
            $css .= '
            .error-404{
                background-image: url("' . esc_url($sparta_pages_404_bg_img) . '");
            }
            ';
        }
        if ($sparta_css_animation_type == '1') {
            $css .= '
                    .sparta-loader-1 .sparta-loader {
                        width: 40px;
                        height: 40px;
                        -webkit-animation: loader-1 1.2s infinite ease-in-out;
                        animation: loader-1 1.2s infinite ease-in-out;
                        background: ' . esc_attr($sparta_css_icon_color) . ';
                        display: inline-block;
                    }
                    @-webkit-keyframes loader-1 {
                        0% {
                            -webkit-transform: perspective(120px) rotateX(0deg) rotateY(0deg);
                            transform: perspective(120px) rotateX(0deg) rotateY(0deg);
                        }
                        50% {
                            -webkit-transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg);
                            transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg);
                        }
                        100% {
                            -webkit-transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg);
                            transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg);
                        }
                    }
                    @keyframes loader-1 {
                        0% {
                            -webkit-transform: perspective(120px) rotateX(0deg) rotateY(0deg);
                            transform: perspective(120px) rotateX(0deg) rotateY(0deg);
                        }
                        50% {
                            -webkit-transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg);
                            transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg);
                        }
                        100% {
                            -webkit-transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg);
                            transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg);
                        }
                    }
                ';

        }

        if ($sparta_css_animation_type == '2') {
            $css .= '
                .sparta-loader-2 .sparta-loader:after,  .sparta-loader-2 .sparta-loader {
                border-radius: 50%;
                width: 6em;
                height: 6em;
                }
                .sparta-loader-2 .sparta-loader{
                    margin: 60px auto;
                    font-size: 10px;
                    position: relative;
                    text-indent: -9999em;
                    border-top: 1.1em solid rgba(255, 255, 255, 0.2);
                    border-right: 1.1em solid rgba(255, 255, 255, 0.2);
                    border-bottom: 1.1em solid rgba(255, 255, 255, 0.2);
                    border-left: 1.1em solid #ffffff;
                    -webkit-transform: translateZ(0);
                    -ms-transform: translateZ(0);
                    transform: translateZ(0);
                    -webkit-animation: load8 1.1s infinite linear;
                    animation: load8 1.1s infinite linear;
                }
                @-webkit-keyframes load8 {
                  0% {
                    -webkit-transform: rotate(0deg);
                    transform: rotate(0deg);
                  }
                  100% {
                    -webkit-transform: rotate(360deg);
                    transform: rotate(360deg);
                  }
                }
                @keyframes load8 {
                  0% {
                    -webkit-transform: rotate(0deg);
                    transform: rotate(0deg);
                  }
                  100% {
                    -webkit-transform: rotate(360deg);
                    transform: rotate(360deg);
                  }
                }  
            ';
        }

        if ($sparta_css_animation_type == '3') {
            $css .= '               
                .sparta-loader-3 .sparta-loader,
                .sparta-loader-3 .sparta-loader:before,
                .sparta-loader-3 .sparta-loader:after {
                  background: ' . esc_attr($sparta_css_icon_color) . ';
                  -webkit-animation: load1 1s infinite ease-in-out;
                  animation: load1 1s infinite ease-in-out;
                  width: 1em;
                  height: 4em;
                }
                .sparta-loader-3 .sparta-loader {
                  color: ' . esc_attr($sparta_css_icon_color) . ';
                  text-indent: -9999em;
                  margin: 88px auto;
                  position: relative;
                  font-size: 11px;
                  -webkit-transform: translateZ(0);
                  -ms-transform: translateZ(0);
                  transform: translateZ(0);
                  -webkit-animation-delay: -0.16s;
                  animation-delay: -0.16s;
                }
                .sparta-loader-3 .sparta-loader:before,
                .sparta-loader-3 .sparta-loader:after {
                  position: absolute;
                  top: 0;
                  content: "";
                }
                .sparta-loader-3 .sparta-loader:before {
                  left: -1.5em;
                  -webkit-animation-delay: -0.32s;
                  animation-delay: -0.32s;
                }
                .sparta-loader-3 .sparta-loader:after {
                  left: 1.5em;
                }
                @-webkit-keyframes load1 {
                  0%,
                  80%,
                  100% {
                    box-shadow: 0 0;
                    height: 4em;
                  }
                  40% {
                    box-shadow: 0 -2em;
                    height: 5em;
                  }
                }
                @keyframes load1 {
                  0%,
                  80%,
                  100% {
                    box-shadow: 0 0;
                    height: 4em;
                  }
                  40% {
                    box-shadow: 0 -2em;
                    height: 5em;
                  }
                }  
            ';
        }

        $css .= '
            .post-thumbnail-gallery a.bggallery:before{
                background-color: rgba(' . $color_primary_rgb["r"] . ', ' . $color_primary_rgb["g"] . ', ' . $color_primary_rgb["b"] . ', 0.6);
            }
            ';

        return $css;
    }


    /**
     * Admin Styles
     */
    public static function sparta_admin_enqueue_scripts()
    {
        wp_register_style('sparta-font-awesome', 'https://use.fontawesome.com/e8cbfd9eca.css', array(), '4.7.0');
        wp_register_script('youtube-api', 'https://www.youtube.com/iframe_api', array());
        wp_enqueue_style('sparta-admin', get_parent_theme_file_uri() . '/assets/css/admin/admin.css', array(), Sparta_Core::VERSION);

    }

    /**
     * Get assets URL
     */
    public static function get_assets_url()
    {
        return trailingslashit(get_parent_theme_file_uri()) . 'assets/';
    }


    public static function sparta_hexToRgb($hex, $alpha = false)
    {
        $hex = str_replace('#', '', $hex);
        $length = strlen($hex);
        $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
        $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
        $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
        if ($alpha) {
            $rgb['a'] = $alpha;
        }
        return $rgb;
    }
}