diff --git a/README.md b/README.md index 46e27fa..abf16e4 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ import { mycolor } from "ohcolor"; import mycolor from "ohcolor"; mycolor("#ff3399").rgba(); // [255, 51, 153, 1] +mycolor("#ff3399").red(); // 255 mycolor(255, 165, 0, 1).red(133).rgba(); // [133, 165, 0, 1] mycolor(255, 165, 0, 1).green(34).rgba(); // [255, 34, 0, 1] mycolor(255, 165, 0, 1).blue(99).rgba(); // [255, 165, 99, 1] diff --git a/src/index.ts b/src/index.ts index bf7d177..efe4e99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -104,9 +104,13 @@ abstract class Color { interface RGB = "rgb"> { /** @description Get rgba array by standard methods */ rgba(): TColorRGB; + red(): RGB_R; red(r: RGB_R): Color; + green(): RGB_G; green(g: RGB_G): Color; + blue(): RGB_B; blue(b: RGB_B): Color; + alpha(): Alpha; alpha(a: Alpha): Color; } @@ -170,19 +174,39 @@ class RGBColor = "rgb"> return [this.r, this.g, this.b, this.a]; } - red(r: number): RGBColor { + red(): RGB_R; + red(r: RGB_R): RGBColor; + red(r?: RGB_R): RGB_R | RGBColor { + if (U.u(r)) { + return this.r; + } return Utils.w(r, this.g, this.b, this.a); } - green(g: number): RGBColor { + green(): RGB_G; + green(g: RGB_G): RGBColor; + green(g?: RGB_G): RGB_G | RGBColor { + if (U.u(g)) { + return this.g; + } return Utils.w(this.r, g, this.b, this.a); } - blue(b: number): RGBColor { + blue(): RGB_B; + blue(b: RGB_B): RGBColor; + blue(b?: RGB_B): RGB_B | RGBColor { + if (U.u(b)) { + return this.b; + } return Utils.w(this.r, this.g, b, this.a); } - alpha(a: number): RGBColor { + alpha(): Alpha; + alpha(a: Alpha): RGBColor; + alpha(a?: Alpha): Alpha | RGBColor { + if (U.u(a)) { + return this.a; + } return Utils.w(this.r, this.g, this.b, a); } } diff --git a/test/index.test.ts b/test/index.test.ts index b5bcb4b..0716d22 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -29,6 +29,11 @@ describe("mycolor", () => { const color2 = color1.alpha(0.1); expect(color1).not.toEqual(color2); + expect(mycolor(255, 165, 0, 1).red()).toEqual(255); + expect(mycolor(255, 165, 0, 1).green()).toEqual(165); + expect(mycolor(255, 165, 0, 1).blue()).toEqual(0); + expect(mycolor(255, 165, 0, 1).alpha()).toEqual(1); + expect(mycolor(255, 165, 0, 1).red(133).rgba()).toEqual([133, 165, 0, 1]); expect(mycolor(255, 165, 0, 1).green(34).rgba()).toEqual([255, 34, 0, 1]); expect(mycolor(255, 165, 0, 1).blue(99).rgba()).toEqual([255, 165, 99, 1]);