Angular-ready radial color picker with some sleek animations.
npm install angular-radial-color-picker![]()
bash
npm install -S angular-radial-color-picker
`
And in your app:
`javascript
import angular from 'angular';
import colorPicker from 'angular-radial-color-picker';
import 'angular-radial-color-picker/dist/css/color-picker.scss';
angular.module('app', [colorPicker]);
`
Depending on your build tool of choice you have to setup the appropriate Webpack loaders or Rollup plugins. The color picker was tested with the latest versions of sass-loader and rollup-plugin-postcss.
#### UMD version
You can also use the minified sources directly:
`html
`
Back To Top
Options
component has several attributes, all of which are optional. See the example which uses all options.
| Options | Type | Default/Description |
|------------|--------|---------|
| color | Object | Object for initializing/changing the color of the picker. Defaults to red:
{hue: 0, saturation: 100, luminosity: 50, alpha: 1}. |
| on-select | Function | Callback which is triggered when a color is selected. |
| on-color-change | Function | A function to invoke when color is changed (i.e. on rotation). |
| mouse-scroll | Boolean | Use wheel (scroll) event to rotate. Defaults to false. |
| scroll-sensitivity | Number | Amount of degrees to rotate the picker with keyboard and/or wheel.
Defaults to 2 degrees. |
Back To Top
Events
For maximum flexibility the component utilizes the pub/sub pattern. For easier communication a set of events are provided that can even programmatically open or close the picker without interacting with the UI. All events carry the current (selected) color in the event data payload.
| Name | Description |
|------------|-------------|
| color-picker.show | Fires when the color picker is about to show and before any animation is started. |
| color-picker.shown | Fires when the color picker is shown and has finished animating. |
| color-picker.selected | Fires when a color is selected via the middle selector. Event is fired right before hide. |
| color-picker.hide | Fires when the color picker is about to hide and before any animation is started. |
| color-picker.hidden | Fires when the color picker is hidden and has finished animating. |
| color-picker.open | Programatically opens the color picker if it's not already opened. |
| color-picker.close | Programatically closes the color picker if it's not already closed. |
Example:
`javascript
// Assign the selected color to the ViewModel and log it to the console
$scope.$on('color-picker.selected', function(ev, color) {
vm.selectedColor = 'hsla(' + color.hue + ', ' + color.saturation + '%, ' + color.luminosity + '%, ' + color.alpha + ')';
console.log('Selected color:', color);
});
// The good'n'tested "poke-it-with-a-stick" method:
$scope.$emit('color-picker.open');
`
Back To Top
Styling/Sizing
The color picker has a default width/height of 280px, but can also be sized via CSS. For example:
`css
color-picker {
width: 350px;
height: 350px;
}
`
If you want a percentage based size you can use this neat little trick with 1:1 aspect ratio box of 40% width of the parent element:
`css
color-picker {
height: 0;
width: 40%;
padding-bottom: 40%;
}
``
hsla(). How can I use other formats like rgba() or HEX?There's a service you can use - ColorPickerService. It has rgbToHsl() which can be used to map a color to the hsla() type that the color picker expects. There's also hslToHex(), hslToRgb() and rgbToHex() which can be used to convert the output of the color picker to other formats.
We suggest to add a custom slider for saturation and luminosity or use <input type="range">.
color-picker component uses $onChanges to detect changes of the color binding. When using <color-picker color="$ctrl.color"></color-picker> if you change a property of $ctrl.color object $onChanges is not triggered, because angular uses a shallow comparison. To properly update the color you'll have to create a new object with the new values. For example:
$ctrl.color.hue = 42; // won't work
// use the angular helper
$ctrl.color = angular.extend({}, $ctrl.color, { hue: 42 });
// or create the object manually
$ctrl.color = {
hue: 42,
luminosity: $ctrl.color.luminosity,
saturation: $ctrl.color.saturation,
alpha: $ctrl.color.alpha
};
// or use Stage-3 Object Spread properties
$ctrl.color = { ...$ctrl.color, hue: 42 };
// or use Object.assign
$ctrl.color = Object.assign({}, $ctrl.color, { hue: 42 });
[Violation] Added non-passive event listener to a scroll-blocking 'touchmove' event. warning in the console?touchmove is used with preventDefault() to block scrolling on mobile while rotating the color knob. Even the Web Incubator Community Group acknowledges that in some cases a passive event listener can't be used.
It's another non-passive event that could potentially introduce jank on scroll. To rotate the color knob, but stay on the same scrolling position the wheel event is blocked with preventDefault(). Thus, if you really want this feature for your users you'll have to explicitly add the mouse-scroll="true" attribute.