Comments (5)
Could we add a special handling for HEXA, RGBA, and maybe HSLA formats, for example, like this?
That is what I meant in my previous comment when I said that I will look into preserving the original value that's entered by a user when it's a valid color.
from coloris.
That is an effect of the rounding of the alpha value (see issue #134), which means that it is not possible to get the color value #000000d7
with the UI of the color picker. I will look into preserving the original value that's entered by a user though when it's a valid color.
I believe this is due to the a * 255 | 0 conversion in the RGBAToHex function. Is this conversion necessary, or could it be removed?
The line a * 255 | 0
is equivalent to Math.floor(a * 255)
in this case.
Here is a proposed fix that addresses the issue:
Your code is actually just substituting one rounding method with another, and you still won't be able to obtain the full 0 to 255 alpha values. For example, with your code, it's not possible to pick #000000d6
.
from coloris.
It appears that information is being lost twice: first in strToRGBA()
(via ctx.fillStyle
), and second in RGBAToHex()
, as I reported in my initial post. For example, when pasting #000000d6
or rgba(0,0,0,0.839)
(which are the same color) into the picker, ctx.fillStyle converts it to rgba(0,0,0,0.84)
, which then translates to #000000d7
in hex. Based on my experience, this bug requires two fixes: the first one is already provided in the initial post, and the second one needs to address strToRGBA()
. Could we add a special handling for HEXA, RGBA, and maybe HSLA formats, for example, like this?
@@ -867,10 +867,13 @@
* Parse a string to RGBA.
* @param {string} str String representing a color.
* @return {object} Red, green, blue and alpha values.
*/
function strToRGBA(str) {
+ const rgba = tryToParseRGBASpecial(str);
+ if (rgba) return rgba;
+
const regex = /^((rgba)|rgb)[\D]+([\d.]+)[\D]+([\d.]+)[\D]+([\d.]+)[\D]*?([\d.]+|$)/i;
let match, rgba;
// Default to black for invalid color strings
ctx.fillStyle = '#000';
from coloris.
Alright, I looked into this, and doing it properly will add unnecessary complexity to account for all the possible syntax variations of rgb
and hsl
, and when the gain is a virtually imperceivable alpha channel fidelity, I don't think it's worth the effort.
from coloris.
What if the solution handled only HEXA colors? This shouldn't introduce too much complexity but would solve the core issue. Here's a proposal:
function tryToParseRGBASpecial(str) {
const match = /^#([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})$/i.exec(str);
if (!match)
return null;
return {
r: parseInt(match[1], 16),
g: parseInt(match[2], 16),
b: parseInt(match[3], 16),
a: parseInt(match[4], 16) / 255 // 0 - 255 -> 0 - 1
};
}
from coloris.
Related Issues (20)
- Add 'Cancel' button (or any custom button) HOT 5
- Materializecss compatibility HOT 2
- Popup Input Issue HOT 3
- Mouse does not lineup on palette after scrolling in scrollable container HOT 3
- Include SCSS file with variables HOT 10
- Add support for CSS custom properties (variables) HOT 1
- Colorpicker wraps all input fields on page load HOT 6
- I would like to contribute sass/scss to the repository HOT 4
- ARIA: issue with Inline `#clr-color-marker` HOT 6
- Incorrect output value of alpha channel in 8-digit Hex Code HOT 8
- The color picker doesn't stick to the input it scrolls along HOT 2
- Option to not style the input element HOT 8
- Make `el` and `parent` options accept `Node` HOT 12
- colorArea doesn't handle right mouse button HOT 2
- Feature Request: Add Eyedropper HOT 2
- Missing info in docs about setting up thumbnails HOT 4
- Not showing in absolute positioned div HOT 2
- colorArea issue with CSS zoom HOT 1
- Coloris does not call the onChange event if the mouse is released outside the colorArea HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coloris.