Giter VIP home page Giter VIP logo

Comments (13)

bgrins avatar bgrins commented on May 24, 2024

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.

bgrins avatar bgrins commented on May 24, 2024

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.

andythenorth avatar andythenorth commented on May 24, 2024

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.

bgrins avatar bgrins commented on May 24, 2024

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.

le717 avatar le717 commented on May 24, 2024

Doesn't this already exist?

console.log(tinycolor("fff").toHexString());

TinyColor/tinycolor.js

Lines 105 to 106 in 44ac02c

toHexString: function(allow3Char) {
return '#' + this.toHex(allow3Char);

from tinycolor.

bgrins avatar bgrins commented on May 24, 2024

@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.

andythenorth avatar andythenorth commented on May 24, 2024

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.

le717 avatar le717 commented on May 24, 2024

My question is this: are you, @teamrubberandy, wanting to restrict invalid syntax, values, or both?

from tinycolor.

le717 avatar le717 commented on May 24, 2024

Sorry about that, on mobile ATM. Hit the wrong button. :)

from tinycolor.

andythenorth avatar andythenorth commented on May 24, 2024

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.

le717 avatar le717 commented on May 24, 2024

Hmmm... I have an idea for this. Will post later.

from tinycolor.

le717 avatar le717 commented on May 24, 2024

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.

le717 avatar le717 commented on May 24, 2024

@teamrubberandy @bgrins That was one long tomorrow. 😛

Here is my idea for the code behind isValidCSSColor(), which does not use the DOM.

  1. Start by checking the input type (rgb/hsv, hex, keyword)
  2. If keyword, check if it is a key in tinycolor.names and return that (tinycolor.names[color] !== undefined
  3. If hex, a simple value.charAt(0) === "#" should do.
  4. If rgb/hsv, use a regex (that still allows flexible input) to confirm the syntax.
  5. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.