145 lines
2.2 KiB
TypeScript
145 lines
2.2 KiB
TypeScript
import { RGBColor } from './RGBColor';
|
|
import { HSLColor } from './HSLColor';
|
|
import { MathX } from '../math/MathX';
|
|
|
|
export abstract class HTMLColor
|
|
{
|
|
protected _a = 0;
|
|
get a(){ return this._a; }
|
|
set a( value:number ){ this._a = value; }
|
|
|
|
abstract toRGB():RGBColor;
|
|
abstract toHSL():HSLColor;
|
|
abstract copyFrom( color:HTMLColor ):void;
|
|
abstract clone():HTMLColor;
|
|
|
|
get r()
|
|
{
|
|
return this.toRGB().r;
|
|
}
|
|
|
|
set r( value:number )
|
|
{
|
|
let rgb = this.toRGB();
|
|
rgb.r = value;
|
|
this.copyFrom( rgb );
|
|
}
|
|
|
|
get g()
|
|
{
|
|
return this.toRGB().g;
|
|
}
|
|
|
|
set g( value:number )
|
|
{
|
|
let rgb = this.toRGB();
|
|
rgb.g = value;
|
|
this.copyFrom( rgb );
|
|
}
|
|
|
|
get b()
|
|
{
|
|
return this.toRGB().b;
|
|
}
|
|
|
|
set b( value:number )
|
|
{
|
|
let rgb = this.toRGB();
|
|
rgb.b = value;
|
|
this.copyFrom( rgb );
|
|
}
|
|
|
|
get h()
|
|
{
|
|
return this.toHSL().h;
|
|
}
|
|
|
|
set h( value:number )
|
|
{
|
|
let hsl = this.toHSL();
|
|
hsl.h = value;
|
|
this.copyFrom( hsl );
|
|
}
|
|
|
|
get s()
|
|
{
|
|
return this.toHSL().s;
|
|
}
|
|
|
|
set s( value:number )
|
|
{
|
|
let hsl = this.toHSL();
|
|
hsl.s = value;
|
|
this.copyFrom( hsl );
|
|
}
|
|
|
|
get l()
|
|
{
|
|
return this.toHSL().l;
|
|
}
|
|
|
|
set l( value:number )
|
|
{
|
|
let hsl = this.toHSL();
|
|
hsl.l = value;
|
|
this.copyFrom( hsl );
|
|
}
|
|
|
|
lighten( amount:number ):HTMLColor
|
|
{
|
|
this.l = this.l + amount;
|
|
return this;
|
|
}
|
|
|
|
multiplyLuminance( amount:number ):HTMLColor
|
|
{
|
|
this.l = this.l * amount;
|
|
return this;
|
|
}
|
|
|
|
|
|
saturate( amount:number ):HTMLColor
|
|
{
|
|
this.s = this.s + amount;
|
|
return this;
|
|
}
|
|
|
|
shiftHue( amount:number ):HTMLColor
|
|
{
|
|
this.h = MathX.repeat( this.h + amount, 360 );
|
|
return this;
|
|
}
|
|
|
|
fade( amount:number ):HTMLColor
|
|
{
|
|
this.a = this.a + amount;
|
|
return this;
|
|
}
|
|
|
|
get physicalLightness()
|
|
{
|
|
return this.r * 0.3 + this.g * 0.5 + this.b * 0.2;
|
|
}
|
|
|
|
setPhysicalLightness( target:number, amount:number )
|
|
{
|
|
let rgb = this.toRGB();
|
|
let lightness = rgb.physicalLightness;
|
|
let scale = target / lightness;
|
|
scale = scale * amount + ( 1 - amount );
|
|
|
|
rgb.scaleRGB( scale );
|
|
|
|
this.copyFrom( rgb );
|
|
|
|
return this;
|
|
}
|
|
|
|
|
|
equals( other:HTMLColor )
|
|
{
|
|
return this.toHSL().equals( other );
|
|
}
|
|
|
|
|
|
} |