- Integrating an opacity slider onto the default wordpress color picker was choosing difficult SEE HERE.
- I wanted the Opacity to be of a set value.
- I wanted the client to be able to choose their selected color without losing the themes inherent design. so placing an rgba() into the stylesheet was out of the question as the editor would be turned off via the wp-config.php file
- Adding opacity:0.5; to the elements background in the stylesheet made all its child elements also have that opacity.
So, it was by chance I came across this article on Chris Coyiers’ website regarding 8 digit hex codes and the opacity factor that they bring to the table. Although it is not fully cross browser compatable, it is available on Chrome, Firefox, Opera and Safari but not on ie(EVER) or Edge yet !
So this is how I tackled it, it may not be the correct way but it worked for me, if you have any improvements please comment below.
How I used it
//output.php
function cocina_css() {
$handle = defined( 'CHILD_THEME_NAME' ) && CHILD_THEME_NAME ? sanitize_title_with_dashes( CHILD_THEME_NAME ) : 'child-theme';
$color_accent = get_theme_mod( 'cocina_accent_color', cocina_customizer_get_default_accent_color() );
//This is the Primary accent color parameters
$css .= ( cocina_customizer_get_default_accent_color() !== $color_accent ) ? sprintf( '
.front-page-3 .image-section .widget{
background-color: %1$s80; //The 80 after the "%1$s" indicates a 50% opacity, a list of other transparencies are linked at the bottom of the article.
}
', $color_accent ) : '';
//customize.php
function cocina_customizer_get_default_accent_color() {
return '#00000080';
}
// Accent color.
$wp_customize->add_setting(
'cocina_accent_color',
array(
'default' => cocina_customizer_get_default_accent_color(),
)
);
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'cocina_accent_color',
array(
'description' => __( 'Change the color of the background', 'cocina' ),
'label' => __( 'Accent Color', 'cocina' ),
'section' => 'colors',
'settings' => 'cocina_accent_color',
)
)
);
For other opacity/Alpha %’s you can see the list on the right(it’s LONG)
Hope this helps in some way?
Hex | Alpha % |
---|---|
00 | 0% |
01 | 0.4% |
02 | 0.8% |
03 | 1.2% |
04 | 1.6% |
05 | 2% |
06 | 2.4% |
07 | 2.7% |
08 | 3.1% |
09 | 3.5% |
0A | 3.9% |
0B | 4.3% |
0C | 4.7% |
0D | 5.1% |
0E | 5.5% |
0F | 5.9% |
10 | 6.3% |
11 | 6.7% |
12 | 7.1% |
13 | 7.5% |
14 | 7.8% |
15 | 8.2% |
16 | 8.6% |
17 | 9% |
18 | 9.4% |
19 | 9.8% |
1A | 10.2% |
1B | 10.6% |
1C | 11% |
1D | 11.4% |
1E | 11.8% |
1F | 12.2% |
20 | 12.5% |
21 | 12.9% |
22 | 13.3% |
23 | 13.7% |
24 | 14.1% |
25 | 14.5% |
26 | 14.9% |
27 | 15.3% |
28 | 15.7% |
29 | 16.1% |
2A | 16.5% |
2B | 16.9% |
2C | 17.3% |
2D | 17.6% |
2E | 18% |
2F | 18.4% |
30 | 18.8% |
31 | 19.2% |
32 | 19.6% |
33 | 20% |
34 | 20.4% |
35 | 20.8% |
36 | 21.2% |
37 | 21.6% |
38 | 22% |
39 | 22.4% |
3A | 22.7% |
3B | 23.1% |
3C | 23.5% |
3D | 23.9% |
3E | 24.3% |
3F | 24.7% |
40 | 25.1% |
41 | 25.5% |
42 | 25.9% |
43 | 26.3% |
44 | 26.7% |
45 | 27.1% |
46 | 27.5% |
47 | 27.8% |
48 | 28.2% |
49 | 28.6% |
4A | 29% |
4B | 29.4% |
4C | 29.8% |
4D | 30.2% |
4E | 30.6% |
4F | 31% |
50 | 31.4% |
51 | 31.8% |
52 | 32.2% |
53 | 32.5% |
54 | 32.9% |
55 | 33.3% |
56 | 33.7% |
57 | 34.1% |
58 | 34.5% |
59 | 34.9% |
5A | 35.3% |
5B | 35.7% |
5C | 36.1% |
5D | 36.5% |
5E | 36.9% |
5F | 37.3% |
60 | 37.6% |
61 | 38% |
62 | 38.4% |
63 | 38.8% |
64 | 39.2% |
65 | 39.6% |
66 | 40% |
67 | 40.4% |
68 | 40.8% |
69 | 41.2% |
6A | 41.6% |
6B | 42% |
6C | 42.4% |
6D | 42.7% |
6E | 43.1% |
6F | 43.5% |
70 | 43.9% |
71 | 44.3% |
72 | 44.7% |
73 | 45.1% |
74 | 45.5% |
75 | 45.9% |
76 | 46.3% |
77 | 46.7% |
78 | 47.1% |
79 | 47.5% |
7A | 47.8% |
7B | 48.2% |
7C | 48.6% |
7D | 49% |
7E | 49.4% |
7F | 49.8% |
80 | 50.2% |
81 | 50.6% |
82 | 51% |
83 | 51.4% |
84 | 51.8% |
85 | 52.2% |
86 | 52.5% |
87 | 52.9% |
88 | 53.3% |
89 | 53.7% |
8A | 54.1% |
8B | 54.5% |
8C | 54.9% |
8D | 55.3% |
8E | 55.7% |
8F | 56.1% |
90 | 56.5% |
91 | 56.9% |
92 | 57.3% |
93 | 57.6% |
94 | 58% |
95 | 58.4% |
96 | 58.8% |
97 | 59.2% |
98 | 59.6% |
99 | 60% |
9A | 60.4% |
9B | 60.8% |
9C | 61.2% |
9D | 61.6% |
9E | 62% |
9F | 62.4% |
A0 | 62.7% |
A1 | 63.1% |
A2 | 63.5% |
A3 | 63.9% |
A4 | 64.3% |
A5 | 64.7% |
A6 | 65.1% |
A7 | 65.5% |
A8 | 65.9% |
A9 | 66.3% |
AA | 66.7% |
AB | 67.1% |
AC | 67.5% |
AD | 67.8% |
AE | 68.2% |
AF | 68.6% |
B0 | 69% |
B1 | 69.4% |
B2 | 69.8% |
B3 | 70.2% |
B4 | 70.6% |
B5 | 71% |
B6 | 71.4% |
B7 | 71.8% |
B8 | 72.2% |
B9 | 72.5% |
BA | 72.9% |
BB | 73.3% |
BC | 73.7% |
BD | 74.1% |
BE | 74.5% |
BF | 74.9% |
C0 | 75.3% |
C1 | 75.7% |
C2 | 76.1% |
C3 | 76.5% |
C4 | 76.9% |
C5 | 77.3% |
C6 | 77.6% |
C7 | 78% |
C8 | 78.4% |
C9 | 78.8% |
CA | 79.2% |
CB | 79.6% |
CC | 80% |
CD | 80.4% |
CE | 80.8% |
CF | 81.2% |
D0 | 81.6% |
D1 | 82% |
D2 | 82.4% |
D3 | 82.7% |
D4 | 83.1% |
D5 | 83.5% |
D6 | 83.9% |
D7 | 84.3% |
D8 | 84.7% |
D9 | 85.1% |
DA | 85.5% |
DB | 85.9% |
DC | 86.3% |
DD | 86.7% |
DE | 87.1% |
DF | 87.5% |
E0 | 87.8% |
E1 | 88.2% |
E2 | 88.6% |
E3 | 89% |
E4 | 89.4% |
E5 | 89.8% |
E6 | 90.2% |
E7 | 90.6% |
E8 | 91% |
E9 | 91.4% |
EA | 91.8% |
EB | 92.2% |
EC | 92.5% |
ED | 92.9% |
EE | 93.3% |
EF | 93.7% |
F0 | 94.1% |
F1 | 94.5% |
F2 | 94.9% |
F3 | 95.3% |
F4 | 95.7% |
F5 | 96.1% |
F6 | 96.5% |
F7 | 96.9% |
F8 | 97.3% |
F9 | 97.6% |
FA | 98% |
FB | 98.4% |
FC | 98.8% |
FD | 99.2% |
FE | 99.6% |
FF | 100% |
Leave a Reply