Sometimes the value of the opacity is something like 0.834, which has 5 characters, but on the form element definition in color_field_admin.inc this is what we have:

    $element['opacity'] = array(
      '#type' => 'textfield',
      '#title' => t('Opacity'),
      '#description' => t('Between 0 and 1'),
      '#default_value' => $opacity,
      '#size' => 4,
      '#maxlength' => 4,
    );

This leads to a error:

"Opacity cannot be longer than 4 characters but is currently 5 characters long."

Tweaking #size and #maxlength to 5 seems to fix the problem.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

HLopes created an issue. See original summary.

ankur.addweb’s picture

Status: Active » Needs review
FileSize
472 bytes

PFA patch.

Status: Needs review » Needs work

The last submitted patch, 2: opacity_longer_than_4-2805163-2.patch, failed testing.

ankur.addweb’s picture

Can anyone please review and let me know if that works for you all or not?

As the attached patch works for me well!

ron_s’s picture

Ok... if you need a higher level of specificity, why not just hook the element and change it in code? There could be someone in the future who needs 6, or someone else who only wants 2, etc.

HLopes’s picture

@ron_s: Even if patched, the approach you suggested (hook_form_alter) will still work. This issue happened with the Spectrum color picker library, which is mentioned in the module page. That's the only reason I've decided to create this ticket.

@darshi: the patch seems to work OK.

ron_s’s picture

Title: Opacity longer than 4 characters » Spectrum widget returns opacity longer than 4 characters?
Version: 7.x-2.0-beta2 » 7.x-2.x-dev

@HLopes, I'm using the Spectrum color picker too. As far as I can see, it always returns a maximum 4 character opacity value when using Color Field 2.x and the opacity slider. I've tested it maybe 100 times and never more than two decimal places.

What version of Spectrum are you using? I'm on the 1.8.0 release. Are you using the slider, or are you manually typing in an opacity value using the "Show Input" option?

HLopes’s picture

@ron_s: happened with the slider, on 1.8 as well. But I'm not currently able to reproduce it, oddly enough...

ron_s’s picture

@HLopes, one thing you might want to consider is trying this patch I created: https://www.drupal.org/node/2816291#comment-11722337

There is definitely a problem with the module's Spectrum JavaScript code for both the rgb and opacity values. There may have been a use case where incorrect values are being returned because of this problem.

gooddesignusa’s picture

I ran into this issue as well. I ended up fixing it on the JS level. Just round the value to 2 places.

petermallett’s picture

Title: Spectrum widget returns opacity longer than 4 characters? » Spectrum widget returns opacity longer than 4 characters
Version: 7.x-2.x-dev » 8.x-2.x-dev
Status: Needs work » Needs review
FileSize
832 bytes

We're seeing the same issue in 8.x-2.x-dev. It seems the alpha value for the Spectrum widget does need to store up to 5 characters.

I created a D8 version of the first patch from this issue. The other option (limiting what the widget attempts to store to 4 char instead) seems fine, too but because the widget already truncates to max 5 char if you manually input more, this seemed like the right way to go.

NickDickinsonWilde’s picture

Status: Needs review » Closed (outdated)

Already fixed - by changing the opacity field to a numeric field. The opacity on all fields widgets is limited to 100/ths for ease of use. If there is a rare case that someone needs more, they can use a form alter.