Skip to content

Instantly share code, notes, and snippets.

@tiborp
Created June 26, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiborp/7d1435b3e3ae17593753c889bac6fcf6 to your computer and use it in GitHub Desktop.
Save tiborp/7d1435b3e3ae17593753c889bac6fcf6 to your computer and use it in GitHub Desktop.
Extract colors from a CSS or Sass file
<?php
/**
* Extract colors from a CSS or Sass file
*
* @param string $path the path to your CSS variables file
*/
function get_colors( $path ) {
$dir = get_stylesheet_directory();
if ( file_exists( $dir . $path ) ) {
$css_vars = file_get_contents( $dir . $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions
preg_match_all( ' /#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/', $css_vars, $matches );
return $matches[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment