add mode option to ColorPicker ('RGB' or 'HSL')

This commit is contained in:
rolux 2011-11-30 15:45:42 +01:00
parent ec4a1c0605
commit a3abf40221

View file

@ -18,36 +18,39 @@ Ox.ColorPicker = function(options, self) {
var that = Ox.Element({}, self) var that = Ox.Element({}, self)
.defaults({ .defaults({
id: '', id: '',
value: '0, 0, 0' mode: 'RGB',
value: options.mode == 'HSL' ? [0, 1, 0.5] : [0, 0, 0]
}) })
.options(options || {}); .options(options || {});
//Ox.Log('Form', self) //Ox.Log('Form', self)
self.$ranges = []; self.$ranges = [];
self.rgb = ['red', 'green', 'blue'];
self.values = self.options.value.split(', ');
Ox.range(3).forEach(function(i) { Ox.loop(3, function(i) {
self.$ranges[i] = Ox.Range({ self.$ranges[i] = Ox.Range({
arrows: true, arrows: true,
id: self.options.id + Ox.toTitleCase(self.rgb[i]), id: self.options.id + i,
max: 255, max: self.options.mode == 'RGB' ? 255 : i == 0 ? 359 : 1,
size: 328, // 256 + 16 + 40 + 16 size: self.options.mode == 'RGB' ? 328 : 432, // 256|360 + 16 + 40 + 16
step: self.options.mode == 'RGB' || i == 0 ? 1 : 0.01,
thumbSize: 40, thumbSize: 40,
thumbValue: true, thumbValue: true,
trackColors: getColors(i), trackColors: getColors(i),
value: self.values[i] value: self.options.value[i]
}) })
.css({ .css({
position: 'absolute', position: 'absolute',
top: (i * 15) + 'px' top: (i * 15) + 'px'
}) })
.bindEvent('change', function(data) { .bindEvent({
change: function(data) {
change(i, data.value); change(i, data.value);
}
}) })
.appendTo(that); .appendTo(that);
// fixme: make self.$ranges[i].children() work // fixme: make self.$ranges[i].children() work
if (i == 0) { if (i == 0) {
// fixme: this should go into Ox.UI.css
self.$ranges[i].$element.children('input.OxOverlapRight').css({ self.$ranges[i].$element.children('input.OxOverlapRight').css({
MozBorderRadius: 0, MozBorderRadius: 0,
WebkitBorderRadius: 0 WebkitBorderRadius: 0
@ -67,17 +70,16 @@ Ox.ColorPicker = function(options, self) {
that = Ox.Picker({ that = Ox.Picker({
element: that, element: that,
elementHeight: 46, elementHeight: 46,
elementWidth: 328, elementWidth: self.options.mode == 'RGB' ? 328 : 432,
id: self.options.id id: self.options.id
}); });
function change(index, value) { function change(index, value) {
self.values[index] = value; self.options.value[index] = value;
self.options.value = self.values.join(', ');
that.$label.css({ that.$label.css({
background: 'rgb(' + self.options.value + ')' background: 'rgb(' + getRGB(self.options.value).join(', ') + ')'
}); });
Ox.range(3).forEach(function(i) { Ox.loop(3, function(i) {
if (i != index) { if (i != index) {
self.$ranges[i].options({ self.$ranges[i].options({
trackColors: getColors(i) trackColors: getColors(i)
@ -90,14 +92,36 @@ Ox.ColorPicker = function(options, self) {
} }
function getColors(index) { function getColors(index) {
return (
self.options.mode == 'RGB' ? [
Ox.range(3).map(function(i) {
return i == index ? 0 : self.options.value[i];
}),
Ox.range(3).map(function(i) {
return i == index ? 255 : self.options.value[i];
})
]
: index == 0 ? Ox.range(7).map(function(i) {
return [i * 60, self.options.value[1], self.options.value[2]];
})
: Ox.range(3).map(function(i) {
return [ return [
'rgb(' + Ox.range(3).map(function(v) { self.options.value[0],
return v == index ? 0 : self.values[v]; index == 1 ? i / 2 : self.options.value[1],
}).join(', ') + ')', index == 2 ? i / 2 : self.options.value[2]
'rgb(' + Ox.range(3).map(function(v) {
return v == index ? 255 : self.values[v];
}).join(', ') + ')'
]; ];
})
).map(function(values) {
return 'rgb(' + getRGB(values).join(', ') + ')';
});
}
function getRGB(values) {
return self.options.mode == 'RGB'
? values
: Ox.rgb(values).map(function(value) {
return Math.round(value);
});
} }
return that; return that;