From 86792bc5aa643c5581ba83484851c328577a2cb2 Mon Sep 17 00:00:00 2001 From: Matthias <18034092+matthias-ccri@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:14:07 -0400 Subject: [PATCH 1/2] Add failing rgba float test --- test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test.js b/test.js index d600342..710ca3f 100644 --- a/test.js +++ b/test.js @@ -329,6 +329,16 @@ Deno.test("RGB Text Parsing", function () { "#ff0000", "parenthesized spaced input" ); + assertEquals( + tinycolor("rgba(255, 25.5, 0, 0.5)").toRgb(), + { + r: 255, + g: 25.5, + b: 0, + a: 0.5, + }, + "rgba with float" + ); assertEquals( tinycolor({ r: 255, g: 0, b: 0 }).toHexString(), "#ff0000", From 3864c94b8ea19d820a9488fe9b744006057de4ca Mon Sep 17 00:00:00 2001 From: Matthias <18034092+matthias-ccri@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:48:42 -0400 Subject: [PATCH 2/2] Implement toRgbValues --- README.md | 6 ++++++ test.js | 10 +++++----- tinycolor.js | 8 ++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 71b6d76..9afbb98 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,12 @@ color.toHex8String(); // "#ff0000ff" var color = tinycolor("red"); color.toRgb(); // { r: 255, g: 0, b: 0, a: 1 } ``` +### toRgbValues +Like `toRgb` but values are not rounded. +```js +var color = tinycolor("rgb(33.3, 66.7, 1)"); +color.toRgbValues(); // { r: 33.3, g: 66.7, b: 1, a: 1 } +``` ### toRgbString ```js var color = tinycolor("red"); diff --git a/test.js b/test.js index 710ca3f..5241ee1 100644 --- a/test.js +++ b/test.js @@ -330,14 +330,14 @@ Deno.test("RGB Text Parsing", function () { "parenthesized spaced input" ); assertEquals( - tinycolor("rgba(255, 25.5, 0, 0.5)").toRgb(), + tinycolor("rgba(33.3, 66.7, 1, 0.5)").toRgbValues(), { - r: 255, - g: 25.5, - b: 0, + r: 33.3, + g: 66.7, + b: 1, a: 0.5, }, - "rgba with float" + "toRgbValues" ); assertEquals( tinycolor({ r: 255, g: 0, b: 0 }).toHexString(), diff --git a/tinycolor.js b/tinycolor.js index e52a3d5..7d8acde 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -137,6 +137,14 @@ a: this._a }; }, + toRgbValues: function toRgbValues() { + return { + r: this._r, + g: this._g, + b: this._b, + a: this._a + }; + }, toRgbString: function toRgbString() { return this._a == 1 ? "rgb(" + Math.round(this._r) + ", " + Math.round(this._g) + ", " + Math.round(this._b) + ")" : "rgba(" + Math.round(this._r) + ", " + Math.round(this._g) + ", " + Math.round(this._b) + ", " + this._roundA + ")"; },