Jump to content

FieldtypeColor


kixe
 Share

Recommended Posts

@adrian Thanks for pointing on this. I added a fallback to the standard dechex() function for 64-bit systems.

In the rare case that neither the BCMath extension is installed nor the system can handle 64-bit integers, the module is not installable.

  • Like 1
Link to comment
Share on other sites

Thanks @kixe

One more request for you please.

I am using this setup:

image.png.a4fa403e7f33b8fa2e5543a4fd1d6ab3.png

$('#{id}').on('keyup', function() {
       $(this).css({'background-color' : $(this).val()});
       $(this).css({'color' : ($(this).val().replace('#','0x')) > (0xffffff/2) ? '#333' : '#fff'});
});

so that the input background changed dynamically as the hex code changes,

ESRXOQoY8z.gif.23b0188b6054738aad30649379515c58.gif

but I also want the background to be colored onload so I changed case 4 to this:

            case 4:
                $this->attr('style', "color: $textColor; background: $bgColor;");
                if ($this->alpha) $this->attr('value', $color32);
                else $this->attr('value', $color24);
                break;

I guess I could probably modify the JS code to do the onload as well, but I was wondering if you thought such a setup would be a worthwhile core option?

  • Thanks 1
Link to comment
Share on other sites

Another nice option might be to extend spectrum to do this. I just did this via browser dev tools, but I expect it could be done with JS quite easily if we has the option to inject JS when using the Inputfield with Spectrum Color Picker (JavaScript) option.

I really like being able to see the hex code without having to open the picker.

What do you think?

image.png.a529d237c984b5954c378bc30c398876.png

image.png.fe1a1b9dc83c09207725a5ec0d78c1bb.png

Link to comment
Share on other sites

2 hours ago, adrian said:

What do you think?

As you know I tested the module with several different javascript based color pickers via the included custom script option. I will leave this as is. Use and modify the picker you like.

Another thing is the dynamically change of the input and background color in the Textfield. This is pretty good and if I find time I will implement this.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...
  • 9 months later...
On 1/12/2019 at 5:42 PM, slave said:

I have a new problem Mr.
rgba(0, 56, 119, 1.)
but I need it so
rgba(0, 56, 119, 1) or rgba(0, 56, 119, 1.0)

Everything fixed. Current module version 1.1.3

Link to comment
Share on other sites

  • 2 months later...

I ran into a specific multilanguage problem.
When setting the default language C parameter of LanguageSupport to nl_NL.UTF-8 (or nl_BE.UTF-8),
then the Color module hsl return value has a comma as decimal separator, which is not a valid CSS value (or a valid hsl value for that matter).

Example with dutch locale: hsl(195, 96,2%, 59%);
Should always be: hsl(195, 96.2%, 59%); no matter the chosen locale.

(note: the comma is the expected/normal decimal separator in dutch, so it's not a LanguageSupport mistake)

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
On 1/11/2020 at 3:00 PM, JFn said:

Example with dutch locale: hsl(195, 96,2%, 59%);
Should always be: hsl(195, 96.2%, 59%); no matter the chosen locale.

This bug is fixed now. Current module version 1.1.4

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

I'm running into an error using the 1.1.4. version.

In field input settings:
Inputfield type = "text" and value type = 8 digits.

In actual page where I'm using this:
If I try to use #ff000000 I get HTML 5 field error stating "Please match the format requested" and saving the page fails.
If I try to use #000000 I get "InputfieldColor: Does not match required pattern: /#?[a-fA-F\d]{8}/"

So something is stopping me from using it with alphavalues. If I go back to using 6 digit hex value it works fine.

EDIT: It would be awesome if I could use RGBA(0,0,0,0) as input value as well. Using the hex value I'll have to google the value each time instead of just typing it out. Another great thing would be, if I could choose "empty" as default value. The forced #000000 may not print anything, but looks very confusing because it looks like someone has typed a value in the field.

Link to comment
Share on other sites

  • 2 weeks later...

@Hurme

On 5/4/2020 at 1:10 PM, Hurme said:

If I try to use #ff000000 I get HTML 5 field error stating "Please match the format requested" and saving the page fails.
If I try to use #000000 I get "InputfieldColor: Does not match required pattern: /#?[a-fA-F\d]{8}/"

This looks like you have choosen the wrong input type:

217156930_Bildschirmfoto2020-05-15um14_14_58.png.08825458e74dd386e1570a08ab5c6174.png

On 5/4/2020 at 1:10 PM, Hurme said:

EDIT: It would be awesome if I could use RGBA(0,0,0,0) as input value as well.

For your needs please use SpectrumColorPicker as Input and add the following code

showAlpha: true, showInput: true 

in the color picker options field. This setup will provide something like:

1579478737_Bildschirmfoto2020-05-15um13_07_40.png.2cce0d91017e7462df0a598a77be9b06.png

 

Link to comment
Share on other sites

  • 1 year later...

Added some useful functions to FieldtypeColor class:
 

/**
 * Find the "naive" difference between two colors.
 * @param int[] $color_a Three-element array with R,G,B color values 0-255.
 * @param int[] $color_b Three-element array with R,G,B color values 0-255.
 * @return int
 */
public function getColorDistance(array $color_a, array $color_b)

/**
 * Find the difference between two colors' luminance values.
 * @param int[] $color_a Three-element array with R,G,B color values 0-255.
 * @param int[] $color_b Three-element array with R,G,B color values 0-255.
 * @return int
 */
public function getLuminanceDistance(array $color_a, array $color_b)

/**
 * Find the closest named color
 * @param hexcolor 6 or 8 digits, with or without leading '#'
 * @return string
 */
public function getClosestColorName(string $color)

Usage example:

$fc = $modules->get('FieldtypeColor');
$colorname = $fc->getClosestColorName('#01fb99');
var_dump($colorname); // string(17) "MediumSpringGreen" 

 

I needed this for the COLOR property in .ics file type. According to the specifications, the value must be a valid CSS3 color name.
https://icalendar.org/New-Properties-for-iCalendar-RFC-7986/5-9-color-property.html

 

  • Like 4
Link to comment
Share on other sites

  • 1 year later...

Hi,

I'm using this in a module config and want to call the value up and pass it through to LESS within the module, but the default format doesn't comply with LESS formatting so - since I don't need alpha for my application is the best method to do as suggested above:

$color_out = '#' . ltrim( $color_in, '#ff' );

Or is there a more elegant way to handle this?

EDIT Okay obviously not that exactly but pulling the rightmost 6 chars and prefixing a #.

Edited by gornycreative
Duh
Link to comment
Share on other sites

$f = wire('modules')->get('InputfieldColor');
$f->inputType = 1;

Simplest solution: Choose inputType 0 or 1.

If you want to use the Spectrum color picker (inputType = 3). I added an option to explicitly disable the alpha channel. Please update to version 1.1.6 and disable the alpha channel:

$f = wire('modules')->get('InputfieldColor');
$f->inputType = 3; // spectrum
$f->alpha = false;

If you want to use a custom JS input type (inputType = 4), the alpha channel is disabled by default. The following additional properties are provided to make it work with your custom color picker:

  • initJS (initial JS)
  • fileJS (custom JS file)
  • fileCSS (custom CSS file)
$f = wire('modules')->get('InputfieldColor');
$f->inputType = 4; // custom js and css
$f->initJS = '$("{id}").initCustomJS({ color: "{value}" });'; // placeholders for value and id of HTML input element
$f->fileJS = "/path/to/file.js";
$f->fileCSS = "/path/to/file.css";
  • Like 1
Link to comment
Share on other sites

On 5/5/2023 at 9:38 PM, gornycreative said:
$color_out = '#' . ltrim( $color_in, '#ff' );

Or is there a more elegant way to handle this?

BTW. Your solution will not work ...

	$color_in = "#ffff0000"; // red with alpha channel
	$color_out = '#' . ltrim( $color_in, '#ff');
	var_dump($color_out); // "#0000" invalid color hex code
	
	$color_out = '#' . substr($color_in, 3);
	var_dump($color_out); // "#ff0000" your expected result

 

Link to comment
Share on other sites

On 5/9/2023 at 2:30 AM, kixe said:

BTW. Your solution will not work ...

Yes I copied from elsewhere in this thread but realized after I posted that it wasn't an actual solution.

On 5/5/2023 at 2:38 PM, gornycreative said:

EDIT Okay obviously not that exactly but pulling the rightmost 6 chars and prefixing a #.

I was under the impression the field always stored values in the db as a 32-bit hex regardless of the output method?

Link to comment
Share on other sites

52 minutes ago, gornycreative said:

I was under the impression the field always stored values in the db as a 32-bit hex regardless of the output method?

FieldtypeColor always stores the value as 32-bit (int) in the database (page field). If we are talking about InputfieldColor, the transmitted value is either a 24- or 32-bit colour hexcode, which depends on the selected inputType as described above. I assume you want to use the input field as a configuration field for your module. In this case, the transmitted value of the input field is relevant. Module configuration data is always stored as a json encoded string in the database.

  • Like 1
Link to comment
Share on other sites

2 hours ago, kixe said:

FieldtypeColor always stores the value as 32-bit (int) in the database (page field). If we are talking about InputfieldColor, the transmitted value is either a 24- or 32-bit colour hexcode, which depends on the selected inputType as described above. I assume you want to use the input field as a configuration field for your module. In this case, the transmitted value of the input field is relevant. Module configuration data is always stored as a json encoded string in the database.

Yes, the input value with or without alpha is important in different contexts, but when I am trying to read the value from the fieldtype I can't directly pass the value to the wikimedia LESS parser as it isn't caught up to the JS version and so if I send a raw 32bit integer to get included in the admin.less process it throws and error as an unacceptable format.

The strPos( $color, 3) solution works fine - I add a # and the LESS parser is happy. I didn't know if any of the output formatting methods from the fieldtype were exposed on the admin side. Not a big deal either way. For my purposes I've got it working.

Link to comment
Share on other sites

  • 2 months later...

Hi @kixe, thank you for this very handy module. I've come up with a situation where I would like access to the $hsl value that lives inside the formatColorString method.
So I would kindly like to request you add a 10th output: the pure HSL value as an indexed array.

 At the FieldtypeColor::___getConfigInputFields method, you could add

$f->addOption(10, $this->_('array([0,360], [69.2%], [56.7%]) indexed array: H,S,L'));

And then the end of the FieldtypeColor::formatColorString method, you could add

if ($of === 10) {
   return $hsl;
}

If you want I can make a pull request for this, but since it's a minor change maybe this way is fine too?
Thanks again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...