wpcom-colors-utils.php 4.9 KB

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