1 line
2.6 KiB
JavaScript
1 line
2.6 KiB
JavaScript
$(function(){"use strict";function a(a,b,c){a/=360,b/=100,c/=100;var d,e,f;if(0==b)d=e=f=c;else{var g=function(a,b,c){return 0>c&&(c+=1),c>1&&(c-=1),1/6>c?a+6*(b-a)*c:.5>c?b:2/3>c?a+(b-a)*(2/3-c)*6:a},h=.5>c?c*(1+b):c+b-c*b,i=2*c-h;d=g(i,h,a+1/3),e=g(i,h,a),f=g(i,h,a-1/3)}return{red:Math.round(255*d),green:Math.round(255*e),blue:Math.round(255*f)}}function b(a,b,c){a/=255,b/=255,c/=255;var d,e,f=Math.max(a,b,c),g=Math.min(a,b,c),h=(f+g)/2;if(f==g)d=e=0;else{var i=f-g;switch(e=h>.5?i/(2-f-g):i/(f+g),f){case a:d=(b-c)/i+(c>b?6:0);break;case b:d=(c-a)/i+2;break;case c:d=(a-b)/i+4}d/=6}return{hue:Math.round(360*d),saturation:Math.round(100*e),luminosity:Math.round(100*h)}}function c(a,b,c){return"#"+((1<<24)+(a<<16)+(b<<8)+c).toString(16).slice(1)}function d(a){var b=parseInt(a.replace("#",""),16),c=b>>16&255,d=b>>8&255,e=255&b;return{red:c,green:d,blue:e}}function e(a){var c=d(a);return b(c.red,c.green,c.blue)}function f(b,d,e){var f=a(b,d,e);return c(f.red,f.green,f.blue)}$.each($(".color-picker"),function(){function c(){var a=h.find(".pin").position(),b=l.width(),c=l.height();return{hue:Math.round(a.left/b*360),saturation:Math.round(a.top/c*100),luminosity:k.val()}}function d(){var b=c();switch(i.val()){case"HSL":j.val("hsl("+b.hue+", "+b.saturation+"%, "+b.luminosity+"%)");break;case"RGB":var d=a(b.hue,b.saturation,b.luminosity);j.val("rgb("+d.red+", "+d.green+", "+d.blue+")");break;case"Hex":j.val(f(b.hue,b.saturation,b.luminosity))}h.trigger("change")}function g(a){var b,c;50>=a?(b="0, 0, 0",c=1-a/100*2):(b="255, 255, 255",c=a/100*2-1),l.children().css("background-color","rgba("+b+", "+c+")")}var h=$(this),i=h.find(".format"),j=h.find(".color"),k=h.find("input[type=range]"),l=h.find(".spectrum"),m=h.find(".pin");i.on("change",function(){d()}),j.on("change",function(){var a;switch(i.val()){case"HSL":var c=$(this).val().match(/\d+/g);a={hue:c[0],saturation:c[1],luminosity:c[2]};break;case"RGB":var c=$(this).val().match(/\d+/g);a=b(c[0],c[1],c[2]);break;case"Hex":a=e($(this).val())}k.val(a.luminosity),g(a.luminosity),m.css({left:a.hue/360*100+"%",top:a.saturation+"%"}),h.trigger("change")}),k.on("change",function(){g($(this).val()),d()});var n=function(a){var b=l.offset(),c=l.width(),e=l.height(),f=a.clientX-b.left,g=a.clientY-b.top;0>f?f=0:f>=c&&(f=c),0>g?g=0:g>=e&&(g=e),m.css({left:f/c*100+"%",top:g/e*100+"%"}),d()};l.on("mousedown",function(a){a.preventDefault(),n(a),l.addClass("active"),$(document).on("mousemove",n)}),$(document).on("mouseup",function(){l.removeClass("active"),$(document).off("mousemove",n)}),l.on("touchmove touchstart",n),h.on("change",function(){j.css("background-color",j.val()).toggleClass("dark",k.val()<=50)}),j.trigger("change")})}); |