wpcom-colors-utils.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. // Custom Colors: Seedlet
  4. function seedlet_define_color_annotations( $colors ) {
  5. // Background Color
  6. // --global--color-background
  7. add_color_rule(
  8. 'bg',
  9. $colors['background'],
  10. array(
  11. // This placeholder is needed to make the color annotations work
  12. array( '.global--color-background', 'background-color' ),
  13. ),
  14. __( 'Background Color', 'seedlet' )
  15. );
  16. // Foreground Color
  17. // --global--color-foreground
  18. add_color_rule(
  19. 'txt',
  20. $colors['foreground'],
  21. array(
  22. // This placeholder is needed to make the color annotations work
  23. array( '.global--color-foreground', 'color' ),
  24. ),
  25. __( 'Foreground Color', 'seedlet' )
  26. );
  27. // Primary Color
  28. // --global--color-primary
  29. add_color_rule(
  30. 'link',
  31. $colors['primary'],
  32. array(
  33. // This placeholder is needed to make the color annotations work
  34. array( '.global--color-primary', 'color' ),
  35. ),
  36. __( 'Primary Color', 'seedlet' )
  37. );
  38. // Secondary Color
  39. // --global--color-secondary
  40. add_color_rule(
  41. 'fg1',
  42. $colors['secondary'],
  43. array(
  44. // Text-color
  45. array( '.global--color-secondary', 'color' ),
  46. ),
  47. __( 'Secondary Color', 'seedlet' )
  48. );
  49. // Tertiary Color
  50. // --global--color-tertiary
  51. add_color_rule(
  52. 'fg2',
  53. $colors['tertiary'],
  54. array(
  55. // Text-color
  56. array( '.global--color-tertiary', 'color' ),
  57. ),
  58. __( 'Tertiary Color', 'seedlet' )
  59. );
  60. }
  61. // These functions are borrowed from the colorline lib
  62. if ( ! function_exists( 'hex_to_rgb' ) ) {
  63. function hex_to_rgb( $hex ) {
  64. return sscanf( $hex, '%02X%02X%02X' );
  65. }
  66. }
  67. // RGB values: 0-255
  68. // LUM values: 0-1
  69. if ( ! function_exists( 'rgb_to_lum' ) ) {
  70. function rgb_to_lum( $rgb ) {
  71. list( $r, $g, $b ) = $rgb;
  72. return sqrt( 0.241 * $r * $r + 0.691 * $g * $g + 0.068 * $b * $b ) / 255;
  73. }
  74. }
  75. // RGB values: 0-255, 0-255, 0-255
  76. // HSV values: 0-360, 0-100, 0-100, 0-100
  77. if ( ! function_exists( 'rgb_to_hsvl' ) ) {
  78. function rgb_to_hsvl( $rgb ) {
  79. $l = rgb_to_lum( $rgb );
  80. list( $r, $g, $b ) = $rgb;
  81. $r = $r / 255;
  82. $g = $g / 255;
  83. $b = $b / 255;
  84. $max_rgb = max( $r, $g, $b );
  85. $min_rgb = min( $r, $g, $b );
  86. $chroma = $max_rgb - $min_rgb;
  87. $v = 100 * $max_rgb;
  88. if ( $chroma > 0) {
  89. $s = 100 * ( $chroma / $max_rgb );
  90. if ( $r === $min_rgb ) {
  91. $h = 3 - ( ( $g - $b ) / $chroma );
  92. } elseif ( $b === $min_rgb ) {
  93. $h = 1 - ( ( $r - $g ) / $chroma );
  94. } else { // $g === $min_rgb
  95. $h = 5 - ( ( $b - $r ) / $chroma );
  96. }
  97. $h = 60 * $h;
  98. return array( $h, $s, $v, $l );
  99. } else {
  100. return array( 0, 0, $v, $l );
  101. }
  102. }
  103. }
  104. if ( ! function_exists( 'change_color_luminescence' ) ) {
  105. function change_color_luminescence( $hex, $amount ) {
  106. $hex_without_hash = substr( $hex, 1, strlen( $hex ) );
  107. $rgb = hex_to_rgb( $hex_without_hash );
  108. $hsvl = rgb_to_hsvl( $rgb );
  109. return 'hsl( ' . $hsvl[0] . ',' . $hsvl[1] . '%,' . ( $hsvl[2] + $amount ) . '%)';
  110. }
  111. }
  112. /**
  113. * Custom CSS.
  114. * The plugin takes the body of this function and applies it in a style tag in the document head.
  115. */
  116. function seedlet_custom_colors_extra_css() {
  117. $colors_array = get_theme_mod( 'colors_manager' );
  118. $background = $colors_array['colors']['bg'];
  119. $foreground = $colors_array['colors']['txt'];
  120. $primary = $colors_array['colors']['link'];
  121. $secondary = $colors_array['colors']['fg1'];
  122. $tertiary = $colors_array['colors']['fg2'];
  123. $foreground_low_contrast = change_color_luminescence( $foreground, 10 );
  124. $foreground_high_contrast = change_color_luminescence( $foreground, -10 );
  125. $primary_hover = change_color_luminescence( $primary, 10 );
  126. $secondary_hover = change_color_luminescence( $secondary, 10 );
  127. ?>
  128. :root,
  129. #editor .editor-styles-wrapper {
  130. --global--color-background: <?php echo $background; ?>;
  131. --global--color-foreground: <?php echo $foreground; ?>;
  132. --global--color-foreground-low-contrast: <?php echo $foreground_low_contrast; ?>;
  133. --global--color-foreground-high-contrast: <?php echo $foreground_high_contrast; ?>;
  134. --global--color-primary: <?php echo $primary; ?>;
  135. --global--color-primary-hover: <?php echo $primary_hover; ?>;
  136. --global--color-secondary: <?php echo $secondary; ?>;
  137. --global--color-secondary-hover: <?php echo $secondary_hover; ?>;
  138. --global--color-tertiary: <?php echo $tertiary; ?>;
  139. }
  140. <?php
  141. }
  142. add_theme_support( 'custom_colors_extra_css', 'seedlet_custom_colors_extra_css' );
  143. /**
  144. * Featured Varia/Seedlet Palettes
  145. */
  146. // Light
  147. add_color_palette(
  148. array(
  149. '#FFFFFF',
  150. '#1D1E1E',
  151. '#C8133E',
  152. '#4E2F4B',
  153. '#F9F9F9',
  154. ), /* translators: This is the name for a color scheme */
  155. 'Light'
  156. );
  157. // Medium
  158. add_color_palette(
  159. array(
  160. '#EEF4F7',
  161. '#242527',
  162. '#35845D',
  163. '#233252',
  164. '#F9F9F9',
  165. ), /* translators: This is the name for a color scheme */
  166. 'Medium'
  167. );
  168. // Dark
  169. add_color_palette(
  170. array(
  171. '#1F2527',
  172. '#FFFFFF',
  173. '#9FD3E8',
  174. '#FBE6AA',
  175. '#364043',
  176. ), /* translators: This is the name for a color scheme */
  177. 'Dark'
  178. );