Comparing version 0.0.1 to 0.0.2
@@ -13,4 +13,8 @@ import { type ColourCode } from './types/ColourCode'; | ||
/** | ||
* Returns the HSL representation of the colour. | ||
* Returns a new Chromator instance with the same colour. | ||
*/ | ||
copy(): Chromator; | ||
/** | ||
* Returns the HSL representation of the colour. Ignores the alpha value. | ||
*/ | ||
getHsl(): Hsl; | ||
@@ -26,3 +30,3 @@ /** | ||
/** | ||
* Returns the RGB representation of the colour. | ||
* Returns the RGB representation of the colour. Ignores the alpha value. | ||
*/ | ||
@@ -35,3 +39,3 @@ getRgb(): Rgb; | ||
/** | ||
* Returns the HSV representation of the colour. | ||
* Returns the HSV representation of the colour. Ignores the alpha value. | ||
*/ | ||
@@ -64,2 +68,12 @@ getHsv(): Hsv; | ||
getHslCode(): string; | ||
/** | ||
* Lightens the colour by a given amount. | ||
* @param amount - The amount of which to increase the lightness on a scale from 0 (nothing) to 1 (white). | ||
*/ | ||
lighten(amount: number): this; | ||
/** | ||
* Darkens the colour by a given amount. | ||
* @param amount - The amount of which to decrease the lightness on a scale from 0 (nothing) to 1 (black). | ||
*/ | ||
darken(amount: number): this; | ||
} |
@@ -25,4 +25,10 @@ "use strict"; | ||
/** | ||
* Returns the HSL representation of the colour. | ||
* Returns a new Chromator instance with the same colour. | ||
*/ | ||
Chromator.prototype.copy = function () { | ||
return new Chromator(this.getHsla()); | ||
}; | ||
/** | ||
* Returns the HSL representation of the colour. Ignores the alpha value. | ||
*/ | ||
Chromator.prototype.getHsl = function () { | ||
@@ -44,3 +50,3 @@ return this.hsl; | ||
/** | ||
* Returns the RGB representation of the colour. | ||
* Returns the RGB representation of the colour. Ignores the alpha value. | ||
*/ | ||
@@ -57,3 +63,3 @@ Chromator.prototype.getRgb = function () { | ||
/** | ||
* Returns the HSV representation of the colour. | ||
* Returns the HSV representation of the colour. Ignores the alpha value. | ||
*/ | ||
@@ -96,2 +102,21 @@ Chromator.prototype.getHsv = function () { | ||
}; | ||
/** | ||
* Lightens the colour by a given amount. | ||
* @param amount - The amount of which to increase the lightness on a scale from 0 (nothing) to 1 (white). | ||
*/ | ||
Chromator.prototype.lighten = function (amount) { | ||
var currentLightness = this.hsl.lightness; | ||
var distanceFromWhite = 1 - currentLightness; | ||
this.hsl.lightness = currentLightness + distanceFromWhite * amount; | ||
return this; | ||
}; | ||
/** | ||
* Darkens the colour by a given amount. | ||
* @param amount - The amount of which to decrease the lightness on a scale from 0 (nothing) to 1 (black). | ||
*/ | ||
Chromator.prototype.darken = function (amount) { | ||
var currentLightness = this.hsl.lightness; | ||
this.hsl.lightness = currentLightness * (1 - amount); | ||
return this; | ||
}; | ||
return Chromator; | ||
@@ -98,0 +123,0 @@ }()); |
{ | ||
"name": "chromator", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "build": "tsc --outDir dist" |
@@ -9,2 +9,15 @@ import { Chromator } from './Chromator'; | ||
describe('copy', () => { | ||
it('Creates a copy of the Chromator', () => { | ||
const chromator = new Chromator('rgb(255, 0, 0)'); | ||
const copy = chromator.copy(); | ||
expect(copy).toBeDefined(); | ||
expect(copy).not.toBe(chromator); | ||
expect(copy.getHsl()).not.toBe(chromator.getHsl()); | ||
expect(copy.getHsl()).toEqual(chromator.getHsl()); | ||
expect(copy.getHsla()).not.toBe(chromator.getHsla()); | ||
expect(copy.getHsla()).toEqual(chromator.getHsla()); | ||
}); | ||
}); | ||
test('getHsl', () => { | ||
@@ -65,2 +78,30 @@ const chromator = new Chromator('rgb(255, 0, 0)'); | ||
}); | ||
describe('lighten', () => { | ||
it('Lightens the colour by the given amount', () => { | ||
const chromator = new Chromator('rgb(255, 0, 0)'); | ||
chromator.lighten(0.5); | ||
expect(chromator.getHsl()).toEqual({ hue: 0, saturation: 1, lightness: 0.75 }); | ||
}); | ||
it('Makes the colour white if the amount is 1', () => { | ||
const chromator = new Chromator('rgb(255, 0, 0)'); | ||
chromator.lighten(1); | ||
expect(chromator.getRgb()).toEqual({ red: 255, green: 255, blue: 255 }); | ||
}); | ||
}); | ||
describe('darken', () => { | ||
it('Darkens the colour by the given amount', () => { | ||
const chromator = new Chromator('rgb(255, 0, 0)'); | ||
chromator.darken(0.5); | ||
expect(chromator.getHsl()).toEqual({ hue: 0, saturation: 1, lightness: 0.25 }); | ||
}); | ||
it('Makes the colour black if the amount is 1', () => { | ||
const chromator = new Chromator('rgb(255, 0, 0)'); | ||
chromator.darken(1); | ||
expect(chromator.getRgb()).toEqual({ red: 0, green: 0, blue: 0 }); | ||
}); | ||
}); | ||
}); |
@@ -20,3 +20,3 @@ import { type ColourCode } from './types/ColourCode'; | ||
constructor (code: ColourCode) { | ||
constructor(code: ColourCode) { | ||
const { hue, saturation, lightness, alpha } = colourCodeToHsla(code); | ||
@@ -28,5 +28,12 @@ this.hsl = { hue, saturation, lightness }; | ||
/** | ||
* Returns the HSL representation of the colour. | ||
* Returns a new Chromator instance with the same colour. | ||
*/ | ||
public getHsl (): Hsl { | ||
public copy(): Chromator { | ||
return new Chromator(this.getHsla()); | ||
} | ||
/** | ||
* Returns the HSL representation of the colour. Ignores the alpha value. | ||
*/ | ||
public getHsl(): Hsl { | ||
return this.hsl; | ||
@@ -38,3 +45,3 @@ } | ||
*/ | ||
public getAlpha (): number { | ||
public getAlpha(): number { | ||
return this.alpha; | ||
@@ -46,3 +53,3 @@ } | ||
*/ | ||
public getHsla (): Hsla { | ||
public getHsla(): Hsla { | ||
return { ...this.hsl, alpha: this.alpha }; | ||
@@ -52,5 +59,5 @@ } | ||
/** | ||
* Returns the RGB representation of the colour. | ||
* Returns the RGB representation of the colour. Ignores the alpha value. | ||
*/ | ||
public getRgb (): Rgb { | ||
public getRgb(): Rgb { | ||
return hslToRgb(this.hsl); | ||
@@ -62,3 +69,3 @@ } | ||
*/ | ||
public getRgba (): Rgba { | ||
public getRgba(): Rgba { | ||
return { ...this.getRgb(), alpha: this.alpha }; | ||
@@ -68,5 +75,5 @@ } | ||
/** | ||
* Returns the HSV representation of the colour. | ||
* Returns the HSV representation of the colour. Ignores the alpha value. | ||
*/ | ||
public getHsv (): Hsv { | ||
public getHsv(): Hsv { | ||
return hslToHsv(this.hsl); | ||
@@ -78,3 +85,3 @@ } | ||
*/ | ||
public getHsva (): Hsva { | ||
public getHsva(): Hsva { | ||
return hslaToHsva(this.getHsla()); | ||
@@ -89,3 +96,3 @@ } | ||
*/ | ||
public getRgbCode (): string { | ||
public getRgbCode(): string { | ||
return rgbaObjectToRgbDecimalString(this.getRgba()); | ||
@@ -100,3 +107,3 @@ } | ||
*/ | ||
public getHexCode (): string { | ||
public getHexCode(): string { | ||
return rgbaObjectToRgbHexString(this.getRgba()); | ||
@@ -111,5 +118,26 @@ } | ||
*/ | ||
public getHslCode (): string { | ||
public getHslCode(): string { | ||
return hslaObjectToHslString(this.getHsla()); | ||
} | ||
/** | ||
* Lightens the colour by a given amount. | ||
* @param amount - The amount of which to increase the lightness on a scale from 0 (nothing) to 1 (white). | ||
*/ | ||
public lighten(amount: number): this { | ||
const currentLightness = this.hsl.lightness; | ||
const distanceFromWhite = 1 - currentLightness; | ||
this.hsl.lightness = currentLightness + distanceFromWhite * amount; | ||
return this; | ||
} | ||
/** | ||
* Darkens the colour by a given amount. | ||
* @param amount - The amount of which to decrease the lightness on a scale from 0 (nothing) to 1 (black). | ||
*/ | ||
public darken(amount: number): this { | ||
const currentLightness = this.hsl.lightness; | ||
this.hsl.lightness = currentLightness * (1 - amount); | ||
return this; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
279428
136
4694
1
59
0