acf_field_color_picker::string_to_array
Convert either a Hexadecimal or RGBA string to an RGBA array.
Метод класса: acf_field_color_picker{}
Хуков нет.
Возвращает
Массив.
Использование
// private - только в коде основоного (родительского) класса $result = $this->string_to_array( $value );
- $value(строка) (обязательный)
- .
Список изменений
| С версии 5.10 | Введена. |
Код acf_field_color_picker::string_to_array() acf field color picker::string to array ACF 6.4.2
private function string_to_array( $value ) {
$value = is_string( $value ) ? trim( $value ) : '';
// Match and collect r,g,b values from 6 digit hex code. If there are 4
// match-results, we have the values we need to build an r,g,b,a array.
preg_match( '/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i', $value, $matches );
if ( count( $matches ) === 4 ) {
return array(
'red' => hexdec( $matches[1] ),
'green' => hexdec( $matches[2] ),
'blue' => hexdec( $matches[3] ),
'alpha' => (float) 1,
);
}
// Match and collect r,g,b values from 3 digit hex code. If there are 4
// match-results, we have the values we need to build an r,g,b,a array.
// We have to duplicate the matched hex digit for 3 digit hex codes.
preg_match( '/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i', $value, $matches );
if ( count( $matches ) === 4 ) {
return array(
'red' => hexdec( $matches[1] . $matches[1] ),
'green' => hexdec( $matches[2] . $matches[2] ),
'blue' => hexdec( $matches[3] . $matches[3] ),
'alpha' => (float) 1,
);
}
// Attempt to match an rgba(…) or rgb(…) string (case-insensitive), capturing the decimals
// as a string. If there are two match results, we have the RGBA decimal values as a
// comma-separated string. Break it apart and, depending on the number of values, return
// our formatted r,g,b,a array.
preg_match( '/^rgba?\(([0-9,.]+)\)/i', $value, $matches );
if ( count( $matches ) === 2 ) {
$decimals = explode( ',', $matches[1] );
// Handle rgba() format.
if ( count( $decimals ) === 4 ) {
return array(
'red' => (int) $decimals[0],
'green' => (int) $decimals[1],
'blue' => (int) $decimals[2],
'alpha' => (float) $decimals[3],
);
}
// Handle rgb() format.
if ( count( $decimals ) === 3 ) {
return array(
'red' => (int) $decimals[0],
'green' => (int) $decimals[1],
'blue' => (int) $decimals[2],
'alpha' => (float) 1,
);
}
}
return array(
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => (float) 0,
);
}