Completed
Push — master ( 361a80...57c765 )
by Atanas
07:10
created

Color   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 27
rs 10
wmc 2
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A hex_to_rgba() 0 17 2
1
<?php
2
3
namespace Carbon_Fields\Helper;
4
5
/**
6
 * Color functions
7
 */
8
class Color {
9
	/**
10
	 * Converts a hexadecimal color into it's RGBA components
11
	 * Accepts hex with and without alpha: #112233, #112233FF
12
	 * Accepts hex with and without a leading # sign
13
	 *
14
	 * @param  string $hex
15
	 * @return array
16
	 */
17
	public static function hex_to_rgba( $hex ) {
18
		$hex = str_replace( '#', '', $hex );
19
		$hex = strlen( $hex ) > 6 ? $hex : $hex . 'FF';
20
21
		$int = hexdec( $hex );
22
		$red = ( $int >> 24 ) & 255;
23
		$green = ( $int >> 16 ) & 255;
24
		$blue = ( $int >> 8 ) & 255;
25
		$alpha = floatval( $int & 255 ) / 255;
26
27
		return array(
28
			'red' => $red,
29
			'green' => $green,
30
			'blue' => $blue,
31
			'alpha' => $alpha,
32
		);
33
	}
34
}
35