Further simplify Color code by moving components definitions to one object.

This commit is contained in:
Jürg Lehni 2011-05-18 21:21:28 +01:00
parent a17f7865a3
commit 106719597b
4 changed files with 22 additions and 23 deletions

View file

@ -15,6 +15,12 @@
*/
var Color = this.Color = Base.extend(new function() {
var components = {
gray: ['gray'],
rgb: ['red', 'green', 'blue'],
hsb: ['hue', 'saturation', 'brightness']
};
var colorCache = {},
colorContext;
@ -207,11 +213,11 @@ var Color = this.Color = Base.extend(new function() {
*/
extend: function(src) {
src.beans = true;
return this.base(
Base.each(src._components || [], function(name) {
// Skip alpha since we have our own version already.
if (name === 'alpha')
return;
if (src._colorType) {
var comps = components[src._colorType];
// Automatically produce the _components field, adding alpha
src._components = comps.concat(['alpha']);
Base.each(comps, function(name) {
var part = Base.capitalize(name),
name = '_' + name,
set = 'set' + part;
@ -228,7 +234,8 @@ var Color = this.Color = Base.extend(new function() {
};
}
}, src)
);
}
return this.base(src);
}
}
};
@ -237,12 +244,8 @@ var Color = this.Color = Base.extend(new function() {
// possible color types. Requesting any of these components on any color
// internally converts the color to the required type and then returns its
// component, using bean access.
Base.each({
rgb: ['red', 'green', 'blue'],
hsb: ['hue', 'saturation', 'brightness'],
gray: ['gray']
}, function(components, colorType) {
Base.each(components, function(component) {
Base.each(components, function(comps, colorType) {
Base.each(comps, function(component) {
var part = Base.capitalize(component);
fields['get' + part] = function() {
return this.convert(colorType)[component];
@ -267,12 +270,11 @@ var Color = this.Color = Base.extend(new function() {
},
getComponents: function() {
var l = this._components.length;
var components = new Array(l);
for (var i = 0; i < l; i++) {
components[i] = this['_' + this._components[i]];
}
return components;
var length = this._components.length;
var comps = new Array(length);
for (var i = 0; i < length; i++)
comps[i] = this['_' + this._components[i]];
return comps;
},
/**

View file

@ -15,6 +15,5 @@
*/
var GrayColor = this.GrayColor = Color.extend({
_colorType: 'gray',
_components: ['gray', 'alpha'],
_colorType: 'gray'
});

View file

@ -16,7 +16,6 @@
var HSBColor = this.HSBColor = Color.extend({
_colorType: 'hsb',
_components: ['hue', 'saturation', 'brightness', 'alpha'],
// Hue needs a special setter, bug getter is produced for it in Color.extend
// No need to set beans: true here since Color.extend() does that for us.

View file

@ -15,6 +15,5 @@
*/
var RGBColor = this.RGBColor = Color.extend({
_colorType: 'rgb',
_components: ['red', 'green', 'blue', 'alpha']
_colorType: 'rgb'
});