📄 moorainbow.js
字号:
/*** * MooRainbow * * @version 1.2b1 * @license MIT-style license * @author Djamil Legato (w00fz) - < w00fzIT [at] gmail.com > * @infos http://moorainbow.woolly-sheep.net * @copyright Author * * includes a fix for mootools 1.2 by Piotr Przybylski */var MooRainbow = new Class({ Implements: [Options, Events], options: { id: 'mooRainbow', prefix: 'moor-', imgPath: 'images/', startColor: [255, 0, 0], wheel: false, onComplete: Class.empty, onChange: Class.empty, selectText: 'Select' }, initialize: function(el, options) { this.element = $(el); if (!this.element) return; this.setOptions(options); this.sliderPos = 0; this.pickerPos = {x: 0, y: 0}; this.backupColor = this.options.startColor; this.currentColor = this.options.startColor; this.sets = { rgb: [], hsb: [], hex: [] }; this.pickerClick = this.sliderClick = false; if (!this.layout) this.doLayout(); this.OverlayEvents(); this.sliderEvents(); this.backupEvent(); if (this.options.wheel) this.wheelEvents(); this.element.addEvent('click', function(e) { this.toggle(e); }.bind(this)); this.layout.overlay.setStyle('background-color', this.options.startColor.rgbToHex()); this.layout.backup.setStyle('background-color', this.backupColor.rgbToHex()); this.pickerPos.x = this.snippet('curPos').l + this.snippet('curSize', 'int').w; this.pickerPos.y = this.snippet('curPos').t + this.snippet('curSize', 'int').h; this.manualSet(this.options.startColor); this.pickerPos.x = this.snippet('curPos').l + this.snippet('curSize', 'int').w; this.pickerPos.y = this.snippet('curPos').t + this.snippet('curSize', 'int').h; this.sliderPos = this.snippet('arrPos') - this.snippet('arrSize', 'int'); if (Browser.Engine.webkit) this.hide(); }, toggle: function() { this[this.visible ? 'hide' : 'show']() }, show: function() { this.rePosition(); this.layout.setStyle('display', 'block'); this.visible = true; }, hide: function() { this.layout.setStyles({'display': 'none'}); this.visible = false; }, manualSet: function(color, type) { if (!type || (type != 'hsb' && type != 'hex')) type = 'rgb'; var rgb, hsb, hex; if (type == 'rgb') { rgb = color; hsb = color.rgbToHsb(); hex = color.rgbToHex(); } else if (type == 'hsb') { hsb = color; rgb = color.hsbToRgb(); hex = rgb.rgbToHex(); } else { hex = color; rgb = color.hexToRgb(true); hsb = rgb.rgbToHsb(); } this.setMooRainbow(rgb); this.autoSet(hsb); }, autoSet: function(hsb) { var curH = this.snippet('curSize', 'int').h; var curW = this.snippet('curSize', 'int').w; var oveH = this.layout.overlay.height; var oveW = this.layout.overlay.width; var sliH = this.layout.slider.height; var arwH = this.snippet('arrSize', 'int'); var hue; var posx = Math.round(((oveW * hsb[1]) / 100) - curW); var posy = Math.round(- ((oveH * hsb[2]) / 100) + oveH - curH); var c = Math.round(((sliH * hsb[0]) / 360)); c = (c == 360) ? 0 : c; var position = sliH - c + this.snippet('slider') - arwH; hue = [this.sets.hsb[0], 100, 100].hsbToRgb().rgbToHex(); this.layout.cursor.setStyles({'top': posy, 'left': posx}); this.layout.arrows.setStyle('top', position); this.layout.overlay.setStyle('background-color', hue); this.sliderPos = this.snippet('arrPos') - arwH; this.pickerPos.x = this.snippet('curPos').l + curW; this.pickerPos.y = this.snippet('curPos').t + curH; }, setMooRainbow: function(color, type) { if (!type || (type != 'hsb' && type != 'hex')) type = 'rgb'; var rgb, hsb, hex; if (type == 'rgb') { rgb = color; hsb = color.rgbToHsb(); hex = color.rgbToHex(); } else if (type == 'hsb') { hsb = color; rgb = color.hsbToRgb(); hex = rgb.rgbToHex(); } else { hex = color; rgb = color.hexToRgb(); hsb = rgb.rgbToHsb(); } this.sets = { rgb: rgb, hsb: hsb, hex: hex }; if (!$chk(this.pickerPos.x)) this.autoSet(hsb); this.RedInput.value = rgb[0]; this.GreenInput.value = rgb[1]; this.BlueInput.value = rgb[2]; this.HueInput.value = hsb[0]; this.SatuInput.value = hsb[1]; this.BrighInput.value = hsb[2]; this.hexInput.value = hex; this.currentColor = rgb; this.chooseColor.setStyle('background-color', rgb.rgbToHex()); }, parseColors: function(x, y, z) { var s = Math.round((x * 100) / this.layout.overlay.width); var b = 100 - Math.round((y * 100) / this.layout.overlay.height); var h = 360 - Math.round((z * 360) / this.layout.slider.height) + this.snippet('slider') - this.snippet('arrSize', 'int'); h -= this.snippet('arrSize', 'int'); h = (h >= 360) ? 0 : (h < 0) ? 0 : h; s = (s > 100) ? 100 : (s < 0) ? 0 : s; b = (b > 100) ? 100 : (b < 0) ? 0 : b; return [h, s, b]; }, OverlayEvents: function() { var lim, curH, curW, inputs; curH = this.snippet('curSize', 'int').h; curW = this.snippet('curSize', 'int').w; inputs = $A(this.arrRGB).concat(this.arrHSB, this.hexInput); document.addEvent('click', function() { if(this.visible) this.hide(this.layout); }.bind(this)); inputs.each(function(el) { if(el) { el.addEvent('keydown', this.eventKeydown.bindWithEvent(this, el)); el.addEvent('keyup', this.eventKeyup.bindWithEvent(this, el)); } }, this); [this.element, this.layout].each(function(el) { el.addEvents({ 'click': function(e) { new Event(e).stop();}, 'keyup': function(e) { e = new Event(e); if(e.key == 'esc' && this.visible) this.hide(this.layout); }.bind(this) }, this); }, this); lim = { x: [0 - curW, (this.layout.overlay.width - curW)], y: [0 - curH, (this.layout.overlay.height - curH)] }; this.layout.drag = new Drag(this.layout.cursor, { limit: lim, onStart: this.overlayDrag.bind(this), onDrag: this.overlayDrag.bind(this), snap: 0 }); this.layout.overlay2.addEvent('mousedown', function(e){ e = new Event(e); this.layout.cursor.setStyles({ 'top': e.page.y - this.layout.overlay.getPosition().y - curH, 'left': e.page.x - this.layout.overlay.getPosition().x - curW }); this.overlayDrag(); this.layout.drag.start(e); }.bind(this)); this.okButton.addEvent('click', function() { if(this.currentColor == this.options.startColor) { this.hide(); this.fireEvent('onComplete', [this.sets, this]); } else { this.backupColor = this.currentColor; this.layout.backup.setStyle('background-color', this.backupColor.rgbToHex()); this.hide(); this.fireEvent('onComplete', [this.sets, this]); } }.bind(this)); }, overlayDrag: function() { var curH = this.snippet('curSize', 'int').h; var curW = this.snippet('curSize', 'int').w; this.pickerPos.x = this.snippet('curPos').l + curW; this.pickerPos.y = this.snippet('curPos').t + curH; this.setMooRainbow(this.parseColors(this.pickerPos.x, this.pickerPos.y, this.sliderPos), 'hsb'); this.fireEvent('onChange', [this.sets, this]); }, sliderEvents: function() { var arwH = this.snippet('arrSize', 'int'), lim; lim = [0 + this.snippet('slider') - arwH, this.layout.slider.height - arwH + this.snippet('slider')]; this.layout.sliderDrag = new Drag(this.layout.arrows, { limit: {y: lim}, modifiers: {x: false}, onStart: this.sliderDrag.bind(this), onDrag: this.sliderDrag.bind(this), snap: 0 }); this.layout.slider.addEvent('mousedown', function(e){ e = new Event(e); this.layout.arrows.setStyle( 'top', e.page.y - this.layout.slider.getPosition().y + this.snippet('slider') - arwH ); this.sliderDrag(); this.layout.sliderDrag.start(e); }.bind(this)); }, sliderDrag: function() { var arwH = this.snippet('arrSize', 'int'), hue; this.sliderPos = this.snippet('arrPos') - arwH; this.setMooRainbow(this.parseColors(this.pickerPos.x, this.pickerPos.y, this.sliderPos), 'hsb'); hue = [this.sets.hsb[0], 100, 100].hsbToRgb().rgbToHex(); this.layout.overlay.setStyle('background-color', hue); this.fireEvent('onChange', [this.sets, this]); }, backupEvent: function() { this.layout.backup.addEvent('click', function() { this.manualSet(this.backupColor); this.fireEvent('onChange', [this.sets, this]); }.bind(this)); }, wheelEvents: function() { var arrColors = $A(this.arrRGB).extend(this.arrHSB); arrColors.each(function(el) { el.addEvents({ 'mousewheel': this.eventKeys.bindWithEvent(this, el), 'keydown': this.eventKeys.bindWithEvent(this, el) }); }, this); [this.layout.arrows, this.layout.slider].each(function(el) { el.addEvents({ 'mousewheel': this.eventKeys.bindWithEvent(this, [this.arrHSB[0], 'slider']), 'keydown': this.eventKeys.bindWithEvent(this, [this.arrHSB[0], 'slider']) }); }, this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -