RGBA Field
I am getting addicted with redux.. lol
For hours I try the rgba 'transparent' to work.
based from here http://labs.abeautifulsite.net/jquery-minicolors/
i find out that 'transparent' value will be automatically converted into #AAAAEE
thus, when you check transparent, it will directly add 'transparent' value into the input field but after saving it the 'transparent' string will be converted into '#AAAAEE' from the input field, however, string that will be sent into the database is still 'transparent' since i got this result
Array ( [color] => transparent [alpha] => 1.00 )
but then when you update something from the same section without touching the rgba field, the new input value '#AAAAEE' which is automatically converted from 'transparent' by jquery-minicolors will be send into the database update again.
In conclusion, we can't seem to have a 'Transparent' value using rgba field when working on the same section.
Upon digging deeply,
I manage to make it work with the help of 3 bottles of coffee, a pack of cigarette and hundreds of code expirementation by modifying something on field_color_rgba.js line 79 - 88
//*change from field_color_rgba.js line 79 - 88--------------------------------*/
$('.color_rgba-transparency').on('click', function() {
if ($(this).is(":checked")) {
$('#' + $(this).data('id')).val('transparent');
$('#' + $(this).data('id')).parent().parent().find('.minicolors-swatch-color').attr('style', '');
} else {
if ($('#' + $(this).data('id')).val() === 'transparent') {
$('#' + $(this).data('id')).val('');
}
}
});
//* to --------------------------------*/
$('.color_rgba-transparency').on('click', function() {
// Getting the specific input based from field ID
var pfs = $(this).parent().parent().data('id');
if ($(this).is(":checked")) {
//Set data-opacity attribute to 0.00 when transparent checkbox is check
$('#' + $(this).data('id')).attr('data-opacity','0.00');
//Set hidded input value alpha opacity to 0.00 when transparent checkbox is check
$('#' + pfs + '-alpha').val('0.00');
//Hide .minicolors-swatch-color SPAN when its check
$('#' + $(this).data('id')).parent().parent().find('.minicolors-swatch-color').css('display','none');
} else {
//might need to restore data-opacity attribute and hidden input alpha value when uncheck
//Unhide .minicolors-swatch-color SPAN when its check
$('#' + $(this).data('id')).parent().parent().find('.minicolors-swatch-color').css('display','');
}
//Unhide .minicolors-swatch-color SPAN when its check on redux-color_rgba input focus
$('.redux-color_rgba').on('focus', function() {
$(this).parent().find('.minicolors-swatch-color').css('display','');
});
and in field_color_rgba.php line 81,
//*changed from field_color_rgba.php line 81 --------------------------------*/
if ( $this->value == "transparent" )
//* to --------------------------------*/
if ( $this->value['alpha'] == "0.00" )
I can't assure if it would work properly, but it seems to work without problems on my end.
But I am thinking if we can just modify the compiler and output value that will just output something like transparent instead of rgba(245,156,12,0.00)
I have also managed to make it work, though I can't assure I am absolutely right as I am only started to learn php and jquery for a few months.
Here's what I did
//* changed from class.redux_helpers.php line 39 - 75 --------------------------------*/
public static function hex2rgba($color, $opacity = false) {
$default = 'rgb(0,0,0)';
//Return default if no color provided
if(empty($color))
return $default;
//Sanitize $color if "#" is provided
if ($color[0] == '#' ) {
$color = substr( $color, 1 );
}
//Check if color has 6 or 3 characters and get values
if (strlen($color) == 6) {
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
} elseif ( strlen( $color ) == 3 ) {
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
} else {
return $default;
}
//Convert hexadec to rgb
$rgb = array_map('hexdec', $hex);
//Check if opacity is set(rgba or rgb)
if($opacity){
if(abs($opacity) > 1)
$opacity = 1.0;
$output = 'rgba('.implode(",",$rgb).','.$opacity.')';
} else {
$output = 'rgb('.implode(",",$rgb).')';
}
//Return rgb(a) color string
return $output;
}
//* to --------------------------------*/
public static function hex2rgba($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = $r.','.$g.','.$b;
return $rgb;
}
then in field_color_rgba.php line 99
change from field_color_rgba.php line 99
$style .= $mode.':'.Redux_Helpers::hex2rgba($this->value['color'], $this->value['alpha']).';';
//* to --------------------------------*/
if ( $this->value['alpha'] == "0.00" || empty($this->value['color']) ) {
$style .= $mode.':transparent;';
} elseif (!empty($this->value['color'])) {
$style .= $mode.':rgba('.Redux_Helpers::hex2rgba($this->value['color']).','. $this->value['alpha'].');';
}
I have tested the above code many times and it seems to work properly,
maybe you'd like to check which you decide you want to do and we can have a working(non-beta) RGBA field? ;)
sorry for any improper line on my code, I'm just a novice developer.
Happy Coding,
Cheers, Kenn
Interesting approach. Let me play with it for a bit. :)
Here is an issue I found with this particular setup:
If one clicks the transparency button and then unchecks it, the colour in the picker doesn't restore itself. Any attempt to modify the colour after that, via the picker, does not display unless the alpha slider is re-adjusted.
Here is how to recreate it:
- Load the options panel.
- Check transparency.
- Use the colour picker to try and select a new colour.
- Click the page to close the colour picker.
- Click the transparency checkbox again. No color returns.
If you can knock that out, we could use this code in production, coz it's really cool. :)
Thanks for checking the code Kevin,
I just noticed that after posting the above code. lol
I have fixed that already on my local server with this code on field_color_rgba.js transparency "checked/unckheck" line
$('.color_rgba-transparency').on('click', function() {
var pfs = $(this).parent().parent().data('id');
var op = $(this).parent().parent().find('.minicolors-swatch-color').css('opacity').substring(0,4);
if ($(this).is(":checked")) {
//alert('Damn Opacity: ' + op);
$('#' + $(this).data('id')).attr('data-opacity','0.00');
$('#' + pfs + '-alpha').val('0.00');
$('#' + $(this).data('id')).parent().parent().find('.minicolors-swatch-color').css('display','none');
} else {
$('#' + $(this).data('id')).attr('data-opacity', op);
$('#' + pfs + '-alpha').val(op);
$('#' + $(this).data('id')).parent().parent().find('.minicolors-swatch-color').css('display','');
}
});
#Code Added
//additional variable to get Opacity value from .minicolors-swatch-color span style - opacity
var op = $(this).parent().parent().find('.minicolors-swatch-color').css('opacity').substring(0,4);
//then putting back the old data-opacity value and alpha hidden input value upon uncheck
$('#' + $(this).data('id')).attr('data-opacity', op);
$('#' + pfs + '-alpha').val(op);
Hope it works on you can you try it again please. ;)
Thanks
Sure will. I'll get back to ya.
I also forgot,
I made a modification of the "On-focus" to restore opacity data even if the transparent checkbox is not uncheck.
##from this
//Unhide .minicolors-swatch-color SPAN when its check on redux-color_rgba input focus
$('.redux-color_rgba').on('focus', function() {
$(this).parent().find('.minicolors-swatch-color').css('display','');
});
##to this
//On Input Color Focus
$('.redux-color_rgba').on('focus', function() {
var op = $(this).parent().find('.minicolors-swatch-color').css('opacity').substring(0,4);
// re-store data-opacity value of the input field
$(this).attr('data-opacity', op);
// re-store alpha hidden input value (not really nescessary)
$('#' + $(this).parent().parent().data('id') + '-alpha').val(op);
//unhide .mini-swatch-color
$(this).parent().find('.minicolors-swatch-color').css('display','');
});
Hope that helps,
Cheers
Well dude, I think you nailed it. I've not found any other issue so far. I'll beat up on it for a little while more (trust me, I'm really good at breaking things ::g::) and if it holds, I'll push it into the repo. It's an awesome fix, really. :)
So, since you seem to know your way around jQuery, we have a field called group, which was removed due to it never working properly. The idea is similar to slides, only the field takes any field we offer and is repeatable. The real hiccup is the javascript from the multiple instances of the fields inside the group field itself. We've all taken a shot at fixing it. Currently, a dude by the name of @tpaksu is having it at. How would you feel about taking a look at it, maybe work with him to see if it can actually be done? If you want nitty-gritty details, you can skype me: kevin.provance.
Here is his fork: https://github.com/tpaksu/ReduxFramework should you be curious.
And again, thanks for the stellar job on this field. :) :)
just back from isolation..lol..
Glad it work properly. I hope it will survive :)
Anyway, it seems that tpaksu already done with group field according to his post on another issue, I'll help with the testing
Thanks @SilverKenn and @kprovance .