Git Product home page Git Product logo

spectrum's Introduction

Spectrum

The No Hassle Colorpicker

See the demo and docs: http://bgrins.github.io/spectrum.

I wanted a colorpicker that didn't require images, and that had an API that made sense to me as a developer who has worked with color in a number of applications. I had tried a number of existing plugins, but decided to try and make a smaller, simpler one.

I started using canvas, then switched to CSS gradients, since it turned out to be easier to manage, and provided better cross browser support.

Basic Usage

Head over to the docs for more information. There is a visual demo of the different options hosted at: http://bgrins.github.io/spectrum.

<script src='spectrum.js'></script>
<link rel='stylesheet' href='spectrum.css' />

<input id='colorpicker' />

<script>
$("#colorpicker").spectrum({
    color: "#f00"
});
</script>

npm

Spectrum is registered as package with npm. It can be installed with:

npm install spectrum-colorpicker

Bower

Spectrum is registered as a package with Bower, so it can be pulled down using:

bower install spectrum

Using spectrum with a CDN

CDN provided by cdnjs

<script src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.0/spectrum.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.0/spectrum.min.css">

Continuous Integration

Build Status

Visit https://travis-ci.org/bgrins/spectrum to view the status of the automated tests.

Building Spectrum Locally

If you'd like to download and use the plugin, head over to http://bgrins.github.io/spectrum/ and click the 'Download Zip' button.

If you'd like to run the development version, spectrum uses Grunt to automate the testing, linting, and building. Head over to http://gruntjs.com/getting-started for more information. First, clone the repository, then run:

npm install -g grunt-cli
npm install

# runs jshint and the unit test suite
grunt

# runs jshint, the unit test suite, and builds a minified version of the file.
grunt build

Internationalization

If you are able to translate the text in the UI to another language, please do! You can do so by either filing a pull request or opening an issue with the translation. The existing languages are listed at: https://github.com/bgrins/spectrum/tree/master/i18n.

For an example, see the Dutch translation.

spectrum's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spectrum's Issues

Incorrect anchoring of pop-up display

If you have the picker running in pop-up mode and it's in a dialog, when the dialog scrolls or you move the dialog, the picker stays where it's at. It does not follow the initiator button. (It doesn't behave like on the demo page.)

e.g.

var dialog = $('

')
.html("")
.dialog({
autoOpen: true,
title: 'Basic Dialog',
create: function(event, ui) {
$('#picker').spectrum();
}
});

fire "change" event after loading color from localStorage

My program is changing the background color of an element based on the color selected by the picker. I keep them in sync by listening to the "change" event of the picker.

Right now the "change" event isn't fired after loading in the saved value from localStorage. It would be nice to do that so that I don't have to manually fetch the initial value after loading and duplicate the code from the "change" listener.

onfocus, onchange

I'm trying to use the onfocus or onchange event of the input tag without any succes...

exemple...
<-input type='text' name='thecolorbox' id='thecolorbox' value='#333333' onfocus="document.getElementById('changeevents').value='true';">

I also tried to change the 'change' event with JQuery...

$("#thecolorbox").spectrum({
showInput: true,
showButtons: true,
change: document.getElementById('changeevents').value='true'
});

How could i override the change event?
Thanks

Make the colorpicker function as a input type="color" polyfill

Maybe it could be something like this? With the input as the actual input that was passed in, and no option to have an input at the bottom like it currently is

<div style="border: solid 2px; display:inline-block;margin:0; border-radius: 5px; overflow:hidden; -webkit-user-select:none;" class='clearfix'>
    <div style='float:left; height: 22px; line-height: 22px; padding:3px; background-color: Orange;overflow:hidden; -webkit-user-select:none;'><input value='#f0f000' style='border:none; margin:0; padding:0; background:transparent;width:75px;font-family:Arial; letter-spacing:2px; height:14px;' /></div>
    <div style='float:left; height: 22px; line-height: 22px; padding:3px; width: 12px; overflow:hidden;background:#74b7d4; border-left: solid 1px; -webkit-user-select:none;'>&#9660;</div>
</div>

Code Nits: Implicitly Declared Variables

The following variables are implicitly declared and therefore global. They just need the "var" keyword added:

Lines 943-944:

                    secondHex = s.toHex();
                    secondAlphaHex = Math.round(parseFloat(s.alpha) * 255).toString(16);

Line 1008: (should use "var" here)

        rgb = { r: 0, g: 0, b: 0 };

Line 1029: (remove "var" here)

                var rgb = hslToRgb(color.h, color.s, color.l);

Line 1534: (redundant set of outer parens) [yeah, it's out of place here :-]

        if ((math.abs(n - max) < 0.000001)) {

Version number and/or date included in js file

Would you mind putting a version number and/or release date in the spectrum.js file? This would make it much easier to determine exactly what version is in use, as we have multiple systems our code runs on (production, test, development). Thanks!

Pass jshint

Time to incorporate code linting using grunt.

Code Nits: Duplicate Declarations:

The following declarations duplicate variables already declared, therefore the "var" keyword can be removed:

Line 1008: var rgb = { r: 0, g: 0, b: 0 };

Line 1183: var r, g, b;

Deprecated reference to toHexCss() in docs?

There are several references to toHexCss() in the docs which I believe should be changed to toHexString().

Great work on this wonderful picker! I wish I had found it sooner...

"color" input goes to black on Chrome

Using Mac Chrome 23.0.1271.64 and jQuery 1.7.2 or 1.6.2.

Given an input with type="color", if you pick a color that is transformable to three-character hex (like #ff88cc), the underlying input's value changes to #000000. This may not be obvious until you, for example, save the value to your database and use it to generate CSS for a customer's web page. Dark, dark CSS. For example.

It looks like this is because by default, Spectrum transforms the color to three-character hex (#f8c) if possible. Chrome does not consider that a valid color, but it knows you wanted to set the value to something! So it sets it to #000000.

You can work around this by setting preferredFormat to hex6, but first you have to notice and track down the bug. Would hex6 be a safer default?

I haven't prepared a test case, but you can fake one in Chrome:

  1. Go here: http://bgrins.github.com/spectrum/#toc21
  2. Scroll down to the 'Hex' demo.
  3. Inspect the color picker and look around for <input type="text" name="preferredHex" ...>
  4. Change the type from text to color.
  5. Using Spectrum, change the color to white.
  6. Click "Choose"
  7. In the console, evaluate $("#preferredHex")[0].value

The value should be a synonym for white, but instead it is "#000000".

Can events pass a reference to the actual control instead of or in addition to the color?

Consider the situation where there are a number of color controls on a single web page and we'd like to monitor one of the events for each one of them. For example, it might be setup this way:

$(document).ready(function(){
$(".is-color-field").spectrum({
change: function(color) {
do_this_and_that();
}
});
});

The above block of code associates a change() function with every color control on a page that has the "is-color-field" css class. This user function then wants to do something related to the given control, but we don't know what control changed, only what color it was set to. Could we do something like this:

change: function(ctrl) {}

where "ctrl" is a reference to the color plugin (something like a "this" reference). At this point in time, the programmer's code can get the color or any other property needed.

Without this, we have to assign a unique ID to each color ctrl and set there change() methods individually, for example:

$(document).ready(function(){
$("#id1").spectrum({
change: function(color) {
do_this();
}
});
$("#id2").spectrum({
change: function(color) {
do_that();
}
});
etc..
});

Or am I missing a better technique?

Color support detection returns a false positive with Safari

Even the new Safari 6 doesn't support the color input but the detection method returns true. The way Modernizr detects support is:

  1. Set the input type and see if it sticks (spectrum currently does this).
  2. Set an invalid value (a smiley ":)") and see if it's reset. If it's not reset then it's a regular text field.

The following works for me:

var tempColor = $("<input type='color' value=':)' />")[0],
    supportsColor = (tempColor.type === "color" && tempColor.value != ":)");

input's value doesn't get updated?

Hi!
Does this line "Spectrum will handle setting the value as it changes, so you don't need to add a single extra line of JavaScript." mean that value of spectrum type input gets updated automatically?

I'm on Mac, Chrome 14 and this part doesn't work for me in your example โ€” value still is "lightblue", no matter what color I choose.

Did I misunderstood something?

Code Nits: Duplicate Declarations in getUniqueSelectionPalette()

In the getUniqueSelectionPalette() function, the declaration for variables "i" and "hex" are duplicated. This is because JavaScript has "functional scope" not "block scope". I would (humbly) suggest changing the method to following. While the original code obviously works, it won't pass through JSLint checkers. ;-)

    function getUniqueSelectionPalette() {
        var unique = [];
        var p = selectionPalette;
        var paletteLookup = {};
        var i = 0, j = 0, hex = 0;   <-- NEW, and "var" keywords removed in  front of these below

        if (showPalette) {

            for (i = 0; i < paletteArray.length; i++) {
                for (j = 0; j < paletteArray[i].length; j++) {
                    hex = tinycolor(paletteArray[i][j]).toHexString();
                    paletteLookup[hex] = true;
                }
            }

            for (i = 0; i < p.length; i++) {
                color = tinycolor(p[i]);
                hex = color.toHexString();

                if (!paletteLookup.hasOwnProperty(hex)) {
                    unique.push(p[i]);
                    paletteLookup[hex] = true;
                }
            }
        }

        return unique.reverse().slice(0, opts.maxSelectionSize);
    }

Loading spectrum via a script loader (e.g. yepnope)

I am trying to load spectrum via modernizr and yepnope. It loads spectrum.js correctly, but throws the error "tinycolor is not defined" on line 433. Is there a preferred way to load spectrum as a conditional polyfill. Testing in FireFox 16 Chromium Debian.

Change the z-index

I'm trying to open the color picker on a modal window that is set at z-index:9999. Whenever i click on it, it show at the back of this window so i can't pick a color. How can i change the z-index of the color picker's container?

cancel button forgets preferred color format

When using a showInput showButtons color picker, the input field shows hex values for color when first opened, but on "cancel" and reopen, the input field displays HSV color values instead.

RGB input support

Any plans to implement RGB inputs on the interface? A client is requesting this, and I love your colorpicker... but I'm kinda stumped on how to expand its functionality to allow a user to do this.

If you could even give me a quick explanation of the code so I could do it myself I will contribute it to your project :)

Picker sticks to corners on Safari under OS X

Dragging around in the color picker square, or the hue slider works for a few seconds, but then starts 'sticking' to the four corners of the box, or the bottom/top of the hue slider. After this happens, even closing/opening the color picker doesn't fix things, the page needs to be reloaded - so we are basically only able to pick black, white or red.

This is happening on the provided index.html example page. Tried JQuery 1.6.3 and 1.8.2 - doesn't matter.

Doesn't accept black as an initial color

When my initial input (that gets hidden) has a value of black (as #000000, 000000 or black), the div with class of sp-preview doesn't have a background color set. It works for all other colors. As soon as you click on the button to show the color picker, the black is pulled in as the initial color.

Ability to set the current color programmatically

I'm loving your elegant color picker; thanks for that! One thing I'm missing is the ability to set the current color programmatically, so after the spectrum initialization has taken place. If I'm not mistaken it is currently impossible to do this, right?

Question regarding how plugin is declared

The Spectrum plugin is declared this way:

(function (window, $, undefined) {
...
})(this, jQuery);

But my JSLint checker complains about passing in "this":

"This inspection reports instances of JavaScript this expression occuring outside of object literals or constructor bodies. Such this expressions are legal JavaScript, and reference the top-level "global" JavaScript object, but are largely useless."

Just curious why you wouldn't just do this:

(function ($) {
...
})(jQuery);

Value of input tag doesn't change when a color is picked

Shouldn't the value of the original input control (that gets hidden) be changed when a color is selected? In my case, I want the value to be set for when the form is posted.

I know I can use the change event, but I was surprised that this isn't automatic.

flat issue

I'm having an issue using the flat feature. If I use spectrum with the default settings, it works fine. But I'd like to use it with flat: true to have the selector always open. When I go to flat, it doesn't allow me to move the selector to choose a different color.

One thing I have noticed is that if I pop the firebug panel open or slide it up or down, it will start kick in and working fine, but otherwise it won't work until I do that once.

any ideas what is wrong with it? if it helps, I've put the element inside of a jquery-ui accordion. Like I said though, default implementation works fine, just doesn't allow a change when I use the flat setting.

Event handling in spectrum

Hi,

I have successfully been able to bind change events from multiple colour pickers on my page to a single callback function.

Is there any way from within the callback function to determine which colour picker on my page triggered the event?

Rob

color palette outside viewport when showPaletteOnly = true

Hi,

I have a combination of circumstances which are causing the colour palette to appear outside the viewport on its first activation. Circimstances are as follows:

  • I am attaching the colour picker to a button element (not the recommended input element)
  • The button element always appears very close to the bottom of the viewport
  • I have showPaletteOnly set to true

With this combination of settings, the first time I click the button, the colour palette appears below it. After this first time it seems to correct itself and subsequent activations display the colour palette above the button. Strangely if I remove the showPaletteOnly setting the colour palette appears correctly the first time.

If a fix is too much work, would be happy to apply a style/write some javascript which forces the colour palette to appear above the button (disabling auto detection). Looked through the code, but couldn't figure out how to achieve this.

Providing "titles" along with palette colors

It would be nice to be able to provide a title="Maroon" attribute with each of the colors provided to palette. Hovering over the colors in the picker could then produce a tooltip with the provided "name" of the color (instead of a hex value that mostly only means something to programmers and designers). Given that the most common use of a color picker is to make it easier for "lay people" to select a color without understand hex or HSV or RGB, it would follow that the tooltips for the colors should be more recognizable. I propose a change to the way the palette option is handled: Continue to accept a flat array (or multiple arrays to control rows/columns), but also allow objects with names such as:

palette: {
   "Maroon":"#b03060",
   "Navy":"#000080",
   "Black-ish":"#020001",
}

...and use the provided strings as the tooltips (if present.)

no colors anymore

Hello Brian, I don't know if it's related to the changes you've made recently but the color picker doesn't show any color in the main box(the small box to click to open the color picker). The functionality is all there and when it's open i see all the colours... I pick a colour, then click choose, and the color stays white in the box...
I had my html pointing at your code so i guess any changes you had made could have affect the result.
Did you change something in the initialization?

Code Nits: Unused Local Variables

The following local variables are unused:

Line 6: (function (window, $, undefined) { [undefined]

Line 566: var h = currentHue;

Line 669: var inputWidth = input.outerWidth();

Line 770: var touches = e.originalEvent.touches;

Line 881: function tinycolor(color, opts) { [opts]

stumped why spectrum() not working for me

I'm probably doing something incredibly stupid, but for some reason I can't get spectrum to work for me -- I included the two required files, and used the code exactly as it is in an example, but the color picker simply shows up as a downward arrow that pops open to a text field.
you can see it here: http://jsfiddle.net/jcarlile/KEQ3x/

any ideas what I'm doing wrong?

Thanks for your help!

"False" positive for colour support in Chrome Frame

Chrome Frame supports type="color" and will display the initial widget correctly but you can't actually open it. I can't find any Chromium bugs relating to this and I think it may just be a limitation of Chrome Frame since IE doesn't have any kind of colour picker that could be used.

You can detect Chrome Frame with the following:

var is_chrome_frame = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()) && !!window.externalHost;

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.