wpcom-colors-utils.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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' )
  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' )
  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' )
  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' )
  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' )
  58. );
  59. }
  60. // These functions are borrowed from the colorline lib
  61. function hex_to_rgb( $hex ) {
  62. return sscanf( $hex, '%02X%02X%02X' );
  63. }
  64. // RGB values: 0-255
  65. // LUM values: 0-1
  66. function rgb_to_lum( $rgb ) {
  67. list( $r, $g, $b ) = $rgb;
  68. return sqrt( 0.241 * $r * $r + 0.691 * $g * $g + 0.068 * $b * $b ) / 255;
  69. }
  70. // RGB values: 0-255, 0-255, 0-255
  71. // HSV values: 0-360, 0-100, 0-100, 0-100
  72. function rgb_to_hsvl( $rgb ) {
  73. $l = rgb_to_lum( $rgb );
  74. list( $r, $g, $b ) = $rgb;
  75. $r = $r / 255;
  76. $g = $g / 255;
  77. $b = $b / 255;
  78. $max_rgb = max( $r, $g, $b );
  79. $min_rgb = min( $r, $g, $b );
  80. $chroma = $max_rgb - $min_rgb;
  81. $v = 100 * $max_rgb;
  82. if ( 0 === $chroma ) {
  83. return array( 0, 0, $v, $l );
  84. }
  85. $s = 100 * ( $chroma / $max_rgb );
  86. if ( $r === $min_rgb ) {
  87. $h = 3 - ( ( $g - $b ) / $chroma );
  88. } elseif ( $b === $min_rgb ) {
  89. $h = 1 - ( ( $r - $g ) / $chroma );
  90. } else { // $g === $min_rgb
  91. $h = 5 - ( ( $b - $r ) / $chroma );
  92. }
  93. $h = 60 * $h;
  94. return array( $h, $s, $v, $l );
  95. }
  96. function change_color_luminescence( $hex, $amount ) {
  97. $hex_without_hash = substr( $hex, 1, strlen( $hex ) );
  98. $rgb = hex_to_rgb( $hex_without_hash );
  99. $hsvl = rgb_to_hsvl( $rgb );
  100. return 'hsl( ' . $hsvl[0] . ',' . $hsvl[1] . '%,' . ( $hsvl[2] + $amount ) . '%)';
  101. }
  102. /**
  103. * Custom CSS.
  104. * The plugin takes the body of this function and applies it in a style tag in the document head.
  105. */
  106. function seedlet_custom_colors_extra_css() {
  107. $colors_array = get_theme_mod( 'colors_manager' );
  108. $background = $colors_array['colors']['bg'];
  109. $foreground = $colors_array['colors']['txt'];
  110. $primary = $colors_array['colors']['link'];
  111. $secondary = $colors_array['colors']['fg1'];
  112. $tertiary = $colors_array['colors']['fg2'];
  113. $foreground_low_contrast = change_color_luminescence( $foreground, 10 );
  114. $foreground_high_contrast = change_color_luminescence( $foreground, -10 );
  115. $primary_hover = change_color_luminescence( $primary, 10 );
  116. $secondary_hover = change_color_luminescence( $secondary, 10 );
  117. ?>
  118. :root,
  119. #editor .editor-styles-wrapper {
  120. --global--color-background: <?php echo $background; ?>;
  121. --global--color-foreground: <?php echo $foreground; ?>;
  122. --global--color-foreground-low-contrast: <?php echo $foreground_low_contrast; ?>;
  123. --global--color-foreground-high-contrast: <?php echo $foreground_high_contrast; ?>;
  124. --global--color-primary: <?php echo $primary; ?>;
  125. --global--color-primary-hover: <?php echo $primary_hover; ?>;
  126. --global--color-secondary: <?php echo $secondary; ?>;
  127. --global--color-secondary-hover: <?php echo $secondary_hover; ?>;
  128. --global--color-tertiary: <?php echo $tertiary; ?>;
  129. }
  130. <?php
  131. }
  132. add_theme_support( 'custom_colors_extra_css', 'seedlet_custom_colors_extra_css' );
  133. /**
  134. * Featured Varia/Seedlet Palettes
  135. */
  136. // Light
  137. add_color_palette(
  138. array(
  139. '#FFFFFF',
  140. '#1D1E1E',
  141. '#C8133E',
  142. '#4E2F4B',
  143. '#F9F9F9',
  144. ), /* translators: This is the name for a color scheme */
  145. 'Light'
  146. );
  147. // Medium
  148. add_color_palette(
  149. array(
  150. '#EEF4F7',
  151. '#242527',
  152. '#35845D',
  153. '#233252',
  154. '#F9F9F9',
  155. ), /* translators: This is the name for a color scheme */
  156. 'Medium'
  157. );
  158. // Dark
  159. add_color_palette(
  160. array(
  161. '#1F2527',
  162. '#FFFFFF',
  163. '#9FD3E8',
  164. '#FBE6AA',
  165. '#364043',
  166. ), /* translators: This is the name for a color scheme */
  167. 'Dark'
  168. );