PluginProbe
Property Hive / 1.4.5
Property Hive v1.4.5
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / ph-formatting-functions.php

ph-formatting-functions.php in Property Hive 1.4.5, at includes/ph-formatting-functions.php

149 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Formatting
4 *
5 * Functions for formatting data.
6 *
7 * @author PropertyHive
8 * @category Core
9 * @package PropertyHive/Functions
10 * @version 1.0.0
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Clean variables using sanitize_text_field.
19 * @param string|array $var
20 * @return string|array
21 */
22 function ph_clean( $var ) {
23 return is_array( $var ) ? array_map( 'ph_clean', $var ) : sanitize_text_field( $var );
24 }
25
26 if ( ! function_exists( 'ph_rgb_from_hex' ) ) {
27
28 /**
29 * Hex darker/lighter/contrast functions for colours.
30 *
31 * @param mixed $color
32 * @return string
33 */
34 function ph_rgb_from_hex( $color ) {
35 $color = str_replace( '#', '', $color );
36 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
37 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
38
39 $rgb = array();
40 $rgb['R'] = hexdec( $color{0} . $color{1} );
41 $rgb['G'] = hexdec( $color{2} . $color{3} );
42 $rgb['B'] = hexdec( $color{4} . $color{5} );
43
44 return $rgb;
45 }
46 }
47
48 if ( ! function_exists( 'ph_hex_darker' ) ) {
49
50 /**
51 * Hex darker/lighter/contrast functions for colours.
52 *
53 * @param mixed $color
54 * @param int $factor (default: 30)
55 * @return string
56 */
57 function ph_hex_darker( $color, $factor = 30 ) {
58 $base = ph_rgb_from_hex( $color );
59 $color = '#';
60
61 foreach ( $base as $k => $v ) {
62 $amount = $v / 100;
63 $amount = round( $amount * $factor );
64 $new_decimal = $v - $amount;
65
66 $new_hex_component = dechex( $new_decimal );
67 if ( strlen( $new_hex_component ) < 2 ) {
68 $new_hex_component = "0" . $new_hex_component;
69 }
70 $color .= $new_hex_component;
71 }
72
73 return $color;
74 }
75 }
76
77 if ( ! function_exists( 'ph_hex_lighter' ) ) {
78
79 /**
80 * Hex darker/lighter/contrast functions for colours.
81 *
82 * @param mixed $color
83 * @param int $factor (default: 30)
84 * @return string
85 */
86 function ph_hex_lighter( $color, $factor = 30 ) {
87 $base = ph_rgb_from_hex( $color );
88 $color = '#';
89
90 foreach ( $base as $k => $v ) {
91 $amount = 255 - $v;
92 $amount = $amount / 100;
93 $amount = round( $amount * $factor );
94 $new_decimal = $v + $amount;
95
96 $new_hex_component = dechex( $new_decimal );
97 if ( strlen( $new_hex_component ) < 2 ) {
98 $new_hex_component = "0" . $new_hex_component;
99 }
100 $color .= $new_hex_component;
101 }
102
103 return $color;
104 }
105 }
106
107 if ( ! function_exists( 'ph_light_or_dark' ) ) {
108
109 /**
110 * Detect if we should use a light or dark colour on a background colour.
111 *
112 * @param mixed $color
113 * @param string $dark (default: '#000000')
114 * @param string $light (default: '#FFFFFF')
115 * @return string
116 */
117 function ph_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
118
119 $hex = str_replace( '#', '', $color );
120
121 $c_r = hexdec( substr( $hex, 0, 2 ) );
122 $c_g = hexdec( substr( $hex, 2, 2 ) );
123 $c_b = hexdec( substr( $hex, 4, 2 ) );
124
125 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
126
127 return $brightness > 155 ? $dark : $light;
128 }
129 }
130
131 if ( ! function_exists( 'ph_format_hex' ) ) {
132
133 /**
134 * Format string as hex.
135 *
136 * @param string $hex
137 * @return string
138 */
139 function ph_format_hex( $hex ) {
140
141 $hex = trim( str_replace( '#', '', $hex ) );
142
143 if ( strlen( $hex ) == 3 ) {
144 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
145 }
146
147 return $hex ? '#' . $hex : null;
148 }
149 }