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;
}
}