Soma

Fieldtype ColorPicker

Recommended Posts

ColorPicker

Custom Fieldtype/Inputfield for ProcessWire 2.+

This module gives you a new custom Fieldtype. Let's you select a color using a Colorpicker jQuery Plugin. The color selected will be stored in HEX format uppercase: "EAEAEA";

To use it in your template as a background HEX color, you'd simple output the value and prefix it with a #:

echo "background-color: #" . $page->color;


When creating a new field in the admin, you can set a default value the field should be prefilled with when creating a new page. The field supports a "reset" button to be able to set it back to the default value.
The colorpicker used: ColorPicker jQuery Plugin by Eyecon

Since 1.0.6 the colorpicker supports color swatches to add predefined colors for easy selection. Thanks @Rayden for the implementation.
 

How to install:

  • Download the contents of this repository and put the folder renamed as "ColorPicker" into your site/modules/ folder
  • Login to processwire and got to Modules page and click "Check for new modules". You should see a note that two new modules were found. Install the FieldtypeColorPicker module under "Field" section. This will also install the required InputfieldColorPicker at the same time.
  • Done
  • You can now create a new field with the "ColorPicker" Fieldtype.


Get it from modules section: http://modules.proce...e-color-picker/

pw-colorpicker.jpg

post-100-0-00111200-1376302356_thumb.png

  • Like 8

Share this post


Link to post
Share on other sites

(can't attach/upload files! what's going on?)

You have probably wasted your generous 500kt (heh heh) quota on old forums already: http://processwire.com/talk/index.php?app=core&module=usercp&tab=core&area=attachments

So maybe that limit could be raised, Pete and Ryan?

About your module, great stuff, I am pretty sure this will come handy! Do you save the hex-value or rgb?

Share this post


Link to post
Share on other sites

Soma, great idea and it seems to work great. Nice job.

As for the code in there, I think all looks good except that you've got a lot of 'extra' that you don't need. I don't think there's any reason to extend FieldtypeText (?) so I would just start from a fresh Fieldtype instead:

class FieldtypeColorPicker extends Fieldtype {

 public static function getModuleInfo() {
return array(
  'title' => 'ColorPicker',
  'version' => 100,
  'summary' => 'Field that stores a hex color as string in a text field. Color is picked using a jQuery ColorPicker Plugin by http://www.eyecon.ro/colorpicker/',
  'installs' => 'InputfieldColorPicker'
   );
 }

 public function getBlankValue(Page $page, Field $field) {
return 'FFFFFF';
 }

 public function sanitizeValue(Page $page, Field $field, $value) {
return strtoupper(substr($value, 0, 6));
 }

 public function getInputfield(Page $page, Field $field) {
return $this->modules->get('InputfieldColorPicker');
 }

 public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = 'CHAR(6) NOT NULL'; // i.e. FFFFFF or 333333 (hex color codes)
return $schema;
 }
}

Likewise, your Inputfield had a lot of extra functions and stuff in there that you didn't need. So you could reduce it to this:

<?php
class InputfieldColorPicker extends Inputfield {

 public static function getModuleInfo() {
return array(
  'title' => 'ColorPicker',
  'version' => 100,
  'summary' => 'Provides and ColorPicker interface for defining hex color.',
  'requires' => array("FieldtypeColorPicker")
  );
 }

 public function __construct() {
parent::__construct();
$this->setAttribute('type', 'hidden');
 }

 public function init() {
parent::init();
$this->config->styles->add($this->config->urls->InputfieldColorPicker . "colorpicker/css/colorpicker.css");
$this->config->scripts->add($this->config->urls->InputfieldColorPicker . "colorpicker/js/colorpicker.js");  
 }

 public function ___render() {
$out = "\n<p><div id='ColorPicker_$this->name' style='border:2px solid #444;display:block;width:40px;height:40px;background-color:#".$this->value."'></div>";
$out .= "<input " . $this->getAttributesString() . " /></p>";
$out .= <<<_OUT

<script type='text/javascript'>
$('#ColorPicker_$this->name').ColorPicker({
 color: '#$this->value',
 onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
 },
 onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
 },
 onChange: function (hsb, hex, rgb) {
$('#ColorPicker_$this->name').css('backgroundColor', '#' + hex);
$('#Inputfield_$this->name').val(hex);
 }
});

</script>

_OUT;

return $out;
 }
}

Share this post


Link to post
Share on other sites

@apeisa, I don't know if it got to do with limited space? ... switched to extended post options ... ok now that is funny, that has to be most funny notice I've seen. There's this line I didn't payed attention closely yet but it says: "Used 2.84MB of your 500K global upload quota (Max. single file size: 100MB)" :D

And yes it's stored as hex. I also thought about wether to chose rgb or hex. And I can't really tell as there's so many option possible, so I just went with hex. I don't know if a converted function or a separate fieldtype or a config option would be best. I haven't really put much more time into it for now, but now that it's working it's time to consider some more possible options.

@ryan, you're awesome, you just made my day. Seriously you helped me really a lot just by doing this cleanup. This is great, as I was also thinking about what could still be removed that's not necessary, but I struggled a lot because I'm still learning this stuff. Thanks for the better database shema! Haven't really taken care of that, oh well. Good idea. I will study it to get a little more clear on fieldtypes. Basicly I wasn't sure what really would be needed to create a new one and thought extending text field would be ok, so I copied the text fieldtype code and deleted some stuff but I guess it was a little off still :D I will implement the changes now and update the rep.

Thanks so much for your help!

Share this post


Link to post
Share on other sites

Ok, I cleaned up the code and tested it. I just comited a new version to github.

  • improved database schema
  • removed a lot of not needed functions and code
  • fixed an issue where it wouldn't trigger a change on the hidden form text input, now works with "isdirty" type of js checks, i.e. for save reminder type modules

Thanks again Ryan. I may will get back with some questions... but need some sleep now :D

Share this post


Link to post
Share on other sites

Thanks Soma, nice job. This is a great module!

Share this post


Link to post
Share on other sites

Thanks Adam, didn't really notice somethings wrong (wrong method name) when recently adding version to scripts. It's fixed in latest commit. You should be able to install it sucessfully.

Share this post


Link to post
Share on other sites

Yeah, I noticed, thanks.

If the MM has update function, can you update the version on modules.processwire.com ? I'd love to try that :) [sorry for DP both here and on GH]

Share this post


Link to post
Share on other sites

Yeah, I noticed, thanks.

If the MM has update function, can you update the version on modules.processwire.com ? I'd love to try that :) [sorry for DP both here and on GH]

Yep, it's now updated on directory.

Share this post


Link to post
Share on other sites

Hi,

Great module! Is it possible to give an example on how to use it in the templates? Thanks in advance!

Wim

---------

Found it :)

It's just $page->field_color

Welcome!

Thanks, glad you found it out on your own. I wanted to add that you also need echo it ;)

echo $page->field_color;

Share this post


Link to post
Share on other sites

Call me stupid (please don't, I may cry) but how do you actually use this? Can you use it to style things in the CSS?

Would you need to use inline styles?

Share this post


Link to post
Share on other sites

just an example:

<style>
body{
  background-color: <?=$page->backColor?>;
}
</style>
  • Like 1

Share this post


Link to post
Share on other sites

You could create a styles.php template. And add your colorpicker there.

then on top of that PHP file:

<?php header("Content-type: text/css"); ?>
  • Like 1

Share this post


Link to post
Share on other sites

You could also output it as a js var and use it in scripts.

<script>
 var mycolor = "#<?php echo $page->color?>";
</script>
  • Like 1

Share this post


Link to post
Share on other sites

I wanted to use Soma's Sliders to generate hsl colours.( 3 sliders ) But Older IE needs rgb.

here's just a bunch of code I used/created to manage it.

maybe helpful.

<?php header("Content-type: text/css");
// rgbToHsl
function rgbToHsl ( array $rgb ) {
list( $r, $g, $b ) = $rgb;
$r/= 255;
$g/= 255;
$b/= 255;
$max = max( $r, $g, $b );
$min = min( $r, $g, $b );
$h;
$s;
$l = ( $max + $min )/ 2;
if ( $max == $min ) {
 $h = $s = 0;
} else {
 $d = $max - $min;
 $s = $l > 0.5 ? $d/ ( 2 - $max - $min ) : $d/ ( $max + $min );
 switch( $max ) {
  case $r:
  $h = ( $g - $b )/ $d + ( $g < $b ? 6 : 0 );
  break;
  case $g:
  $h = ( $b - $r )/ $d + 2;
  break;
  case $b:
  $h = ( $r - $g )/ $d + 4;
  break;
 }
 $h/= 6;
}
return array( $h, $s, $l );
}
// hslToRgb
function hslToRgb ( array $hsl ) {
list( $h, $s, $l ) = $hsl;
$r;
$g;
$b;
if ( $s == 0 ) {
 $r = $g = $b = $l;
}
else {
 $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
 $p = 2 * $l - $q;
 $r = hueToRgb( $p, $q, $h + 1/ 3 );
 $g = hueToRgb( $p, $q, $h );
 $b = hueToRgb( $p, $q, $h - 1/ 3 );
}
return array( round( $r * 255 ), round( $g * 255 ), round( $b * 255 ) );
}
// Convert percentages to points (0-255)
function normalizeCssRgb ( array $rgb ) {
foreach ( $rgb as &$val ) {
 if ( strpos( $val, '%' ) !== false ) {
  $val = str_replace( '%', '', $val );
  $val = round( $val * 2.55 );
 }
}
return $rgb;
}
// cssHslToRgb
function cssHslToRgb ( array $hsl ) {
// Normalize the hue degree value then convert to float
$h = array_shift( $hsl );
$h = $h % 360;
if ( $h < 0 ) {
 $h = 360 + $h;
}
$h = $h/ 360;
// Convert s and l to floats
foreach ( $hsl as &$val ) {
 $val = str_replace( '%', '', $val );
 $val/= 100;
}
list( $s, $l ) = $hsl;

$hsl = array( $h, $s, $l );
$rgb = hslToRgb( $hsl );

return $rgb;
}
// hueToRgb
function hueToRgb ( $p, $q, $t ) {
if ( $t < 0 ) $t += 1;
if ( $t > 1 ) $t -= 1;
if ( $t < 1/6 ) return $p + ( $q - $p ) * 6 * $t;
if ( $t < 1/2 ) return $q;
if ( $t < 2/3 ) return $p + ( $q - $p ) * ( 2/ 3 - $t ) * 6;
return $p;
}
// rgbToHex
function rgbToHex ( array $rgb ) {
$hex_out = '#';
foreach ( $rgb as $val ) {
 $hex_out .= str_pad( dechex( $val ), 2, '0', STR_PAD_LEFT );
}
return $hex_out;
}
// hexToRgb
function hexToRgb ( $hex ) {
$hex = substr( $hex, 1 );

// Handle shortened format
if ( strlen( $hex ) === 3 ) {
 $long_hex = array();
 foreach ( str_split( $hex ) as $val ) {
  $long_hex[] = $val . $val;
 }
 $hex = $long_hex;
}
else {
 $hex = str_split( $hex, 2 );
}
return array_map( 'hexdec', $hex );
}
/**
*
* Output to a "r, g, b" string
*
*/
function colorStringRGB($h, $s, $l ) {
$output = '';
$hsl = array($h, $s, $l);
$color = cssHslToRgb($hsl);

foreach( $color as $rgb ) $output .= $rgb . ', ';

$output = rtrim( $output, ', ' );
return $output;
}

// ---------------------------------------------------------------------------------------

$out = '';
$sections = array(
'aa' => 'value-a',
'bb' => 'value-b',
'cc' => 'value-c',
'dd' => 'value-d',
'ee' => 'value-e',
'ff' => 'value-f',
'gg' => 'value-g',
);

/**
*
* Converts HSL to RGB.
*
*
*/
$minH = $page->css_hue->min;
$maxH = $page->css_hue->max;
$count = count($sections);
$steps = round(( $maxH - $minH ) / $count);
$minL = $page->css_lightness->min;
$maxL = $page->css_lightness->max;
// l is defined in the foreach
$h = $page->css_check == 1 ? $minH : $maxH;
$s = $page->css_saturation;

foreach($sections as $key => $value) {
$out .= "\n\nli." . $key . ' {';
$out .= "\n\t" . 'background-color: rgb(' . colorStringRGB($h, ($s+20), $minL) . ');';
$out .= "\n" .  '}';
$out .= "\n";  
$out .= "\n\ndiv." . $key . ' {';
$out .= "\n\t" . 'background-color: rgb(' . colorStringRGB($h, ($s+20), $minL) . ');';
$out .= "\n" .  '}';
$out .= "\n";  
$out .= "\n." . $key . ' .column {			    background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(2n) {  background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(3n) {  background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(5n) {  background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(7n) {  background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(11n) { background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(13n) { background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(17n) { background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column:nth-child(23n) { background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column.r1.c8 {		  background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';
$out .= "\n." . $key . ' .column.r3.c1 {		  background-color: rgb(' . colorStringRGB($h, $s, rand($minL,$maxL)) . '); }';

$h = $page->css_check == 1 ? $h + $steps : $h - $steps;

}

echo $out;

?>
  • Like 1

Share this post


Link to post
Share on other sites

You could create a styles.php template. And add your colorpicker there.

then on top of that PHP file:

<?php header("Content-type: text/css"); ?>

Martijn, can you give any more information on how to get this working. Did you rename your style.css file to style.php and update it in the head tag?

That's what I did but don't think PW likes it as the page is coming up unstyled?

Share this post


Link to post
Share on other sites

OK think I've sorted it you have to store php scripts outside the templates folder.

However, it's not parsing my variables, I'm getting "undefined variable"

Share this post


Link to post
Share on other sites

Just found a possible issue. I can't seem to get colorpicker fields to work from within a fieldset tab?

When I click on the colour, nothing comes up, but the same field works fine outside of a fieldset?

I'm on the latest dev version of PW - not sure if that's relevant.

Share this post


Link to post
Share on other sites

Thanks onjegolders for the report. I just updated the module to also work in tabs. It had to do that the init script was executed immediately before wiretabs.

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By bernhard
      WHY?
      This module was built to fill the gap between simple $pages->find() operations and complex SQL queries.
      The problem with $pages->find() is that it loads all pages into memory and that can be a problem when querying multiple thousands of pages. Even $pages->findMany() loads all pages into memory and therefore is a lot slower than regular SQL.
      The problem with SQL on the other hand is, that the queries are quite complex to build. All fields are separate tables, some repeatable fields use multiple rows for their content that belong to only one single page, you always need to check for the page status (which is not necessary on regular find() operations and therefore nobody is used to that).
      In short: It is far too much work to efficiently and easily get an array of data based on PW pages and fields and I need that a lot for my RockGrid module to build all kinds of tabular data.

      Basic Usage

       
      Docs & Download
      https://modules.processwire.com/modules/rock-finder/
      https://gitlab.com/baumrock/RockFinder/tree/master
       
      Changelog
      180817, v1.0.6, support for joining multiple finders 180810, v1.0.5, basic support for options fields 180528, v1.0.4, add custom select statement option 180516, change sql query method, bump version to 1.0.0 180515, multilang bugfix 180513, beta release <180513, preview/discussion took place here: https://processwire.com/talk/topic/18983-rocksqlfinder-highly-efficient-and-flexible-sql-finder-module/
    • By OLSA
      Hello for all,
      ConfigurationForm fieldtype module is one my experiment from 2016.
      Main target to build this module was to store multiple setup and configuration values in just 1 field and avoid to use 1 db table to store just single "number of items on page", or another db table to store "layout type" etc. Thanks to JSON formatted storage this module can help you to reduce number of PW native fields in project, save DB space, and reduce number of queries at front-end.
      Install and setup:
      Download (at the bottom ), unzip and install like any other PW module (site/modules/...). Create some filed using this type of field (ConfigurationForm Fieldtype) Go to field setup Input tab and drag some subfields to container area (demo). Set "Name" and other params for subfields Save and place field to templates ("Action tab") How to use it:
      In my case, I use it to store setup and configurations values, but also for contact details, small content blocks... (eg. "widgets").
      Basic usage example:
      ConfigForm fieldtype "setup" has subfields:
      "limit", type select, option values: 5, 10, 15, 20
      "sort", type select, option values: "-date", "date",  "-sort", "sort"
      // get page children (items) $limit = isset($page->setup->limit) ? $page->setup->limit : 10; $sort = isset($page->setup->sort) ? $page->setup->sort : '-sort'; $items = $page->children("limit=$limit, sort=$sort");  
      Screenshots:


       
      Notes:
      Provide option to search inside subfields Provide multilanguage inputs for text and textarea field types Provide option for different field layout per-template basis Do not place/use field type "Button" or "File input" because it won't works. Please read README file for more details and examples Module use JSON format to store values. Text and textarea field types are multilanguage compatible, but please note that main target for this module was to store setup values and small content blocks and save DB space. Search part inside JSON is still a relatively new in MySQL (>=5.77) and that's on you how and for what to use this module.
      Thanks:
      Initial point for this fieldtype was jQuery plugin FormBuiled and thanks to Kevin Chappel for this plugin.
      In field type "link" I use javascript part from @marcostoll module and thanks to him for that part.
      Download:
      FieldtypeConfigForm.zip
      Edit: 14. August 2018. please delete/uninstall previously downloaded zip
      Regards.
         
    • By bernhard
      @Sergio asked about the pdf creation process in the showcase thread about my 360° feedback/survey tool and so I went ahead and set my little pdf helper module to public.
      Description from PW Weekly:
       
      Modules Directory: https://modules.processwire.com/modules/rock-pdf/
      Download & Docs: https://gitlab.com/baumrock/RockPdf
       
      You can combine it easily with RockReplacer: 
      See also a little showcase of the RockPdf module in this thread:
       
    • By Thomas Diroll
      Hi guys I'm relatively new to PW and just finished developing a page for a client. I was able to include all necessary functionality using the core fieldtypes but now I it seems that I need to extend them with a custom one. What I need is a simple button, that copies the absolute url (frontend not PW-backend) of the page which is currently edited to the clipboard. As this feature is only needed inside a specific template, I tend to use a custom fieldtype which provides this feature. I've been looking inside the core modules code (eg. FieldtypeCheckbox.module) but I don't really get the structure of it and how its rendered to the admin page. I also didn't find a lot of tutorials covering custom fieldtypes.
      Maybe some of you could give me some tips on how to write a basic custom fieldtype that renders a button which copies the value of
      page->httpUrl() to the clipboard using JS. Thanks!
    • By bernhard
      Some of you might have followed the development of this module here: https://processwire.com/talk/topic/15524-previewdiscussion-rockdatatables/ . It is the successor of "RockDataTables" and requires RockFinder to get the data for the grid easily and efficiently. It uses the open source part of agGrid for grid rendering.
       
      WHY?
      ProcessWire is awesome for creating all kinds of custom backend applications, but where it is not so awesome in my opinion is when it comes to listing this data. Of course we have the built in page lister and we have ListerPro, but none of that solutions is capable of properly displaying large amounts of data, for example lists of revenues, aggregations, quick and easy sorts by the user, instant filter and those kind of features. RockGrid to the rescue 😉 
       
      Features/Highlights:
      100k+ rows Instant (client side) filter, search, sort (different sort based on data type, eg "lower/greater than" for numbers, "contains" for strings) extendable via plugins (available plugins at the moment: fullscreen, csv export, reload, batch-processing of data, column sum/statistics, row selection) all the agGrid features (cell renderers, cell styling, pagination, column grouping etc) vanilla javascript, backend and frontend support (though not all plugins are working on the frontend yet and I don't plan to support it as long as I don't need it myself)  
      Limitations:
      While there is an option to retrieve data via AJAX the actual processing of the grid (displaying, filtering, sorting) is done on the client side, meaning that you can get into troubles when handling really large datasets of several thousands of rows. agGrid should be one of the most performant grid options in the world (see the official example page with a 100k row example) and does a lot to prevent problems (such as virtual row rendering), but you should always have this limitation in mind as this is a major difference to the available lister options that do not have this limitation.
      Currently it only supports AdminThemeUikit and I don't plan to support any other admin theme.
       
      Download: https://gitlab.com/baumrock/RockGrid
      Installation: https://gitlab.com/baumrock/RockGrid/wikis/Installation
      Quikckstart: https://gitlab.com/baumrock/RockGrid/wikis/quickstart
      Further instructions: https://gitlab.com/baumrock/RockGrid/wikis/quickstart#further-instructions
       
      Module status: alpha, License: MIT
      Note that every installation and uninstallation sends an anonymous google analytics event to my google analytics account. If you don't want that feel free to remove the appropriate lines of code before installation/uninstallation.
       
      Contribute:
      You can contribute to the development of this and other modules or just say thank you by
      testing, reporting issues and making PRs at gitlab liking this post buying me a drink: paypal.me/baumrock/5 liking my facebook page: facebook.com/baumrock hiring me for pw work: baumrock.com  
      Support: Please note that this module might not be as easy and plug&play as many other modules. It needs a good understanding of agGrid (and JavaScript in general) and it likely needs some looks into the code to get all the options. Please understand that I can not provide free support for every request here in the forum. I try to answer all questions that might also help others or that might improve the module but for individual requests I offer paid support for 60€ per hour.
       
      Changelog
      180730 support subdir installations 180711 bugfix (naming issue) 180630 alpha realease  
      Use Cases / Examples:
      Colored grid cells, Icons, Links etc. The Grid also has a "batcher" feature built in that helps communicating with the server via AJAX and managing resource intensive tasks in batches:

      Filters, PW panel links and instant reload on panel close:

      You can combine the grid with a chart library like I did with the (outdated) RockDataTables module: