wpcom-colors-utils.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ( 0 === $chroma ) {
  88. return array( 0, 0, $v, $l );
  89. }
  90. $s = 100 * ( $chroma / $max_rgb );
  91. if ( $r === $min_rgb ) {
  92. $h = 3 - ( ( $g - $b ) / $chroma );
  93. } elseif ( $b === $min_rgb ) {
  94. $h = 1 - ( ( $r - $g ) / $chroma );
  95. } else { // $g === $min_rgb
  96. $h = 5 - ( ( $b - $r ) / $chroma );
  97. }
  98. $h = 60 * $h;
  99. return array( $h, $s, $v, $l );
  100. }
  101. }
  102. if ( ! function_exists( 'change_color_luminescence' ) ) {
  103. function change_color_luminescence( $hex, $amount ) {
  104. $hex_without_hash = substr( $hex, 1, strlen( $hex ) );
  105. $rgb = hex_to_rgb( $hex_without_hash );
  106. $hsvl = rgb_to_hsvl( $rgb );
  107. return 'hsl( ' . $hsvl[0] . ',' . $hsvl[1] . '%,' . ( $hsvl[2] + $amount ) . '%)';
  108. }
  109. }
  110. /**
  111. * Custom CSS.
  112. * The plugin takes the body of this function and applies it in a style tag in the document head.
  113. */
  114. function seedlet_custom_colors_extra_css() {
  115. $colors_array = get_theme_mod( 'colors_manager' );
  116. $background = $colors_array['colors']['bg'];
  117. $foreground = $colors_array['colors']['txt'];
  118. $primary = $colors_array['colors']['link'];
  119. $secondary = $colors_array['colors']['fg1'];
  120. $tertiary = $colors_array['colors']['fg2'];
  121. $foreground_low_contrast = change_color_luminescence( $foreground, 10 );
  122. $foreground_high_contrast = change_color_luminescence( $foreground, -10 );
  123. $primary_hover = change_color_luminescence( $primary, 10 );
  124. $secondary_hover = change_color_luminescence( $secondary, 10 );
  125. ?>
  126. :root,
  127. #editor .editor-styles-wrapper {
  128. --global--color-background: <?php echo $background; ?>;
  129. --global--color-foreground: <?php echo $foreground; ?>;
  130. --global--color-foreground-low-contrast: <?php echo $foreground_low_contrast; ?>;
  131. --global--color-foreground-high-contrast: <?php echo $foreground_high_contrast; ?>;
  132. --global--color-primary: <?php echo $primary; ?>;
  133. --global--color-primary-hover: <?php echo $primary_hover; ?>;
  134. --global--color-secondary: <?php echo $secondary; ?>;
  135. --global--color-secondary-hover: <?php echo $secondary_hover; ?>;
  136. --global--color-tertiary: <?php echo $tertiary; ?>;
  137. }
  138. <?php
  139. }
  140. add_theme_support( 'custom_colors_extra_css', 'seedlet_custom_colors_extra_css' );
  141. /**
  142. * Featured Varia/Seedlet Palettes
  143. */
  144. // Light
  145. add_color_palette(
  146. array(
  147. '#FFFFFF',
  148. '#1D1E1E',
  149. '#C8133E',
  150. '#4E2F4B',
  151. '#F9F9F9',
  152. ), /* translators: This is the name for a color scheme */
  153. 'Light'
  154. );
  155. // Medium
  156. add_color_palette(
  157. array(
  158. '#EEF4F7',
  159. '#242527',
  160. '#35845D',
  161. '#233252',
  162. '#F9F9F9',
  163. ), /* translators: This is the name for a color scheme */
  164. 'Medium'
  165. );
  166. // Dark
  167. add_color_palette(
  168. array(
  169. '#1F2527',
  170. '#FFFFFF',
  171. '#9FD3E8',
  172. '#FBE6AA',
  173. '#364043',
  174. ), /* translators: This is the name for a color scheme */
  175. 'Dark'
  176. );