wpcom-colors-css-variables.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * Color palette related utilities
  4. */
  5. require_once get_template_directory() . '/inc/color-utils.php';
  6. // Custom Colors:
  7. function varia_define_color_annotations( $colors ) {
  8. // Background Color
  9. if ( isset( $colors['background'] ) ) {
  10. // --wp--preset--color--background
  11. add_color_rule(
  12. 'bg',
  13. $colors['background'],
  14. array(
  15. // This placeholder is needed to make the color annotations work
  16. array( '.wp--preset--color--background', 'background-color' ),
  17. ),
  18. __( 'Background Color' )
  19. );
  20. }
  21. // Foreground Color
  22. if ( isset( $colors['foreground'] ) ) {
  23. // --wp--preset--color--foreground
  24. add_color_rule(
  25. 'txt',
  26. $colors['foreground'],
  27. array(
  28. // This placeholder is needed to make the color annotations work
  29. array( '.wp--preset--color--foreground', 'color' ),
  30. ),
  31. __( 'Foreground Color' )
  32. );
  33. }
  34. // Primary Color
  35. if ( isset( $colors['primary'] ) ) {
  36. // --wp--preset--color--primary
  37. add_color_rule(
  38. 'link',
  39. $colors['primary'],
  40. array(
  41. // This placeholder is needed to make the color annotations work
  42. array( '.wp--preset--color--primary', 'color' ),
  43. ),
  44. __( 'Primary Color' )
  45. );
  46. }
  47. // Secondary Color
  48. if ( isset( $colors['secondary'] ) ) {
  49. // --wp--preset--color--secondary
  50. add_color_rule(
  51. 'fg1',
  52. $colors['secondary'],
  53. array(
  54. // Text-color
  55. array( '.wp--preset--color--secondary', 'color' ),
  56. ),
  57. __( 'Secondary Color' )
  58. );
  59. }
  60. // Tertiary Color
  61. if ( isset( $colors['tertiary'] ) ) {
  62. // --wp--preset--color--tertiary
  63. add_color_rule(
  64. 'fg2',
  65. $colors['tertiary'],
  66. array(
  67. // Text-color
  68. array( '.wp--preset--color--tertiary', 'color' ),
  69. ),
  70. __( 'Tertiary Color' )
  71. );
  72. }
  73. }
  74. /**
  75. * Custom CSS.
  76. * The plugin takes the body of this function and applies it in a style tag in the document head.
  77. */
  78. function varia_custom_colors_extra_css() {
  79. $colors_array = get_theme_mod( 'colors_manager' );
  80. if ( isset( $colors_array['colors']['bg'] ) ) {
  81. $background = $colors_array['colors']['bg'];
  82. $background_low_contrast = change_color_luminescence( $background, -10 );
  83. $background_high_contrast = change_color_luminescence( $background, 10 );
  84. }
  85. if ( isset( $colors_array['colors']['txt'] ) ) {
  86. $foreground = $colors_array['colors']['txt'];
  87. $foreground_low_contrast = change_color_luminescence( $foreground, 10 );
  88. $foreground_high_contrast = change_color_luminescence( $foreground, -10 );
  89. }
  90. if ( isset( $colors_array['colors']['link'] ) ) {
  91. $primary = $colors_array['colors']['link'];
  92. $primary_hover = change_color_luminescence( $primary, 10 );
  93. $primary_dark = change_color_luminescence( $primary, -10 );
  94. }
  95. if ( isset( $colors_array['colors']['fg1'] ) ) {
  96. $secondary = $colors_array['colors']['fg1'];
  97. $secondary_hover = change_color_luminescence( $secondary, 10 );
  98. }
  99. if ( isset( $colors_array['colors']['fg2'] ) ) {
  100. $border = $colors_array['colors']['fg2'];
  101. $border_low_contrast = change_color_luminescence( $border, 10 );
  102. $border_high_contrast = change_color_luminescence( $border, -10 );
  103. }
  104. ?>
  105. :root,
  106. #editor .editor-styles-wrapper {
  107. <?php if ( isset( $colors_array['colors']['bg'] ) ) { ?>
  108. --wp--preset--color--background: <?php echo $background; ?>;
  109. --wp--preset--color--background-low-contrast: <?php echo $background_low_contrast; ?>;
  110. --wp--preset--color--background-high-contrast: <?php echo $background_high_contrast; ?>;
  111. <?php
  112. }
  113. if ( isset( $colors_array['colors']['txt'] ) ) {
  114. ?>
  115. --wp--preset--color--foreground: <?php echo $foreground; ?>;
  116. --wp--preset--color--foreground-low-contrast: <?php echo $foreground_low_contrast; ?>;
  117. --wp--preset--color--foreground-high-contrast: <?php echo $foreground_high_contrast; ?>;
  118. <?php
  119. }
  120. if ( isset( $colors_array['colors']['link'] ) ) {
  121. ?>
  122. --wp--preset--color--primary: <?php echo $primary; ?>;
  123. --wp--preset--color--primary-hover: <?php echo $primary_hover; ?>;
  124. --wp--preset--color--primary-dark: <?php echo $primary_dark; ?>;
  125. <?php
  126. }
  127. if ( isset( $colors_array['colors']['fg1'] ) ) {
  128. ?>
  129. --wp--preset--color--secondary: <?php echo $secondary; ?>;
  130. --wp--preset--color--secondary-hover: <?php echo $secondary_hover; ?>;
  131. <?php
  132. }
  133. if ( isset( $colors_array['colors']['fg2'] ) ) {
  134. ?>
  135. --wp--preset--color--border: <?php echo $border; ?>;
  136. --wp--preset--color--border-low-contrast: <?php echo $border_low_contrast; ?>;
  137. --wp--preset--color--border-high-contrast: <?php echo $border_high_contrast; ?>;
  138. <?php } ?>
  139. }
  140. <?php
  141. }
  142. add_theme_support( 'custom_colors_extra_css', 'varia_custom_colors_extra_css' );
  143. /**
  144. * Featured Palettes
  145. */
  146. $has_tertiary_color = false;
  147. if ( function_exists( 'varia_default_colors' ) ) {
  148. $default_colors = varia_default_colors();
  149. varia_define_color_annotations( $default_colors );
  150. if ( $default_colors['tertiary'] ) {
  151. $has_tertiary_color = true;
  152. }
  153. }
  154. $light = array(
  155. '#FFFFFF',
  156. '#1D1E1E',
  157. '#C8133E',
  158. '#4E2F4B',
  159. );
  160. $medium = array(
  161. '#EEF4F7',
  162. '#242527',
  163. '#35845D',
  164. '#233252',
  165. );
  166. $dark = array(
  167. '#1F2527',
  168. '#FFFFFF',
  169. '#9FD3E8',
  170. '#FBE6AA',
  171. );
  172. if ( $has_tertiary_color ) {
  173. $light[] = '#F9F9F9';
  174. $medium[] = '#F9F9F9';
  175. $dark[] = '#364043';
  176. }
  177. add_color_palette(
  178. $light,
  179. /* translators: This is the name for a color scheme */
  180. 'Light'
  181. );
  182. add_color_palette(
  183. $medium,
  184. /* translators: This is the name for a color scheme */
  185. 'Medium'
  186. );
  187. add_color_palette(
  188. $dark,
  189. /* translators: This is the name for a color scheme */
  190. 'Dark'
  191. );