Comments (13)
Off the top of my head, I think this could be validated using the DOM before passing it into the tinycolor function. Maybe something like:
function isValidCSSColor(c) {
var foo = document.createElement("div");
foo.style.backgroundColor = c;
return foo.style.backgroundColor !== "";
}
console.log(isValidCSSColor("red")); // true
console.log(isValidCSSColor("#f00")); // true
console.log(isValidCSSColor("f00")); // false
This would also handle cases like hsv or unparenthesized rgb, hsl and unsupported units.
from tinycolor.
I'm not sure how well it would fit into the API, but it might be a handy utility to be able to:
var color = tinycolor("red");
color.isValidCSSColor()
from tinycolor.
isValidCSSColor() would work very well for the case I have, thanks. If it doesn't fit into the API, don't worry about it, the case I have is solved, just it's an ugly solution. :)
from tinycolor.
The only issue with regards to the API is that it would rely on running in a browser environment, and it's supported in node
from tinycolor.
Doesn't this already exist?
console.log(tinycolor("fff").toHexString());
Lines 105 to 106 in 44ac02c
from tinycolor.
@le717 I think the request is for that not to work. Basically, @teamrubberandy is wanting anything that isn't a valid css color to not parse (like "fff"
, "rgb 255 0 0"
, "hsv (0, 100%, 50%)"
, {r:1,g:1,b:1}
, etc)
from tinycolor.
Yup, my aim is to validate user input, and fail if not valid css colors.
The generous parsing behaviour is neat, but the user input in my case is passed on as vars a less compile which then whines at me when it's invalid (or, in production, would blow up all over the user) :)
from tinycolor.
My question is this: are you, @teamrubberandy, wanting to restrict invalid syntax, values, or both?
from tinycolor.
Sorry about that, on mobile ATM. Hit the wrong button. :)
from tinycolor.
For my case, invalid syntax, specifically cases like "fff" and "rgb 255 0 0".
Invalid values are already caught by calling isValid() against user input (and showing failure appropriately to user). :)
from tinycolor.
Hmmm... I have an idea for this. Will post later.
from tinycolor.
I wish I could explain further, but I GTG and can't until tomorrow. :(
Anyway, my idea was to check the input against a regex / charAt(0)
(for Hex) that searched for proper structure, then returned true
or false
on the outcome. The color format would be determined as best as possible before checking against the regex. Rough idea of what I refer to (download and run, sorry, no JSFiddle):
https://gist.github.com/le717/63faf91c76c99fb0952f
Thoughts?
from tinycolor.
@teamrubberandy @bgrins That was one long tomorrow. 😛
Here is my idea for the code behind isValidCSSColor()
, which does not use the DOM.
- Start by checking the input type (rgb/hsv, hex, keyword)
- If keyword, check if it is a key in
tinycolor.names
and return that (tinycolor.names[color] !== undefined
- If hex, a simple
value.charAt(0) === "#"
should do. - If rgb/hsv, use a regex (that still allows flexible input) to confirm the syntax.
- If it is in scope of the method purpose, run the last two though
isValid()
.
My gist linked above is a proof-of-concept way of performing the hex and rgb/hsv validiation. The latter's regex would need expanding to support more whitespace in the value, but as you can see by running the code, it does accurately check the syntax validity.
from tinycolor.
Related Issues (20)
- In built way to automatically Darken or Lighten by amount depending on the color luminance
- Is toFloatString() implemented?
- React 18 types mismatch
- Feature: include alpha in Hex HOT 2
- Is there a way to reverse a colour, like a negative image?
- Support for LCH?
- Octal literal in strict mode
- tinycolor.mix(color1, color2, amount = 50) not working in React native HOT 1
- Doesn't work with calc HOT 1
- tinycolor is not a function HOT 39
- remove `engines` field from package.json
- Task: Publish 1.6.0 to npm
- Remove tinycolor.js and dist/tinycolor-min.js once we confirm they aren't used HOT 1
- Include a Changelog HOT 4
- asset files includes ES6 syntax HOT 2
- Task: Publish 1.6.1
- [Question] Overlay Colors
- why tinycolor({h: 0, s: 100,v: 0}).toHsv() result {h: 0, s: 0, v: 0} ? HOT 2
- [Question] OKLCH support?
- RGBA fails to parse if alpha is missing
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 tinycolor.