Home  >  Article  >  Web Front-end  >  js implementation of color selector examples that can obtain different color values_javascript skills

js implementation of color selector examples that can obtain different color values_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:12:121217browse

The example in this article describes the js implementation of a color selector that can obtain different color values. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:


js color selector, you can get different color values







<script><br> var iW = '70'; //There are 6 colors in total, and the width of each color is iW. iW*6 is the width of the ribbon. <br> var iH = '400'; //iH is the height of the ribbon. <br> //Calculate HSV color code. <br> function HSV(){<br> kkk1.value = 'X:' event.offsetX ' Y:' event.offsetY;<br> var H,S,V;<br> var pY = event.offsetY;<br> if(pY ​​== 0){H = S = 0; V = 100;}<br> else{<br> if(pY ​​== iH-1) H = S = V = 0;<br> else{<br> H = Math.floor(360*event.offsetX/(iW*6));<br> S = Math.round(50*(iH-pY)/(iH/2));<br> V = Math.round(100-50*pY/iH);<br> }<br> }<br> kkk2.value='HSV(' H ',' S '%,' V '%)';<br> HSVtoRGB(H,S/100,V/100);<br> } <p>//Calculate RGB color code. <br> function HSVtoRGB(h,s,v){<br> var i, f ,p1 ,p2 ,p3;<br> var r = g = b = 0;<br> if(s < 0) s=0;<br /> if(s > 1) s=1;<br> if(v < 0) v=0;<br /> if(v > 1) v=1;<br> h %= 360;<br> if(h < 0) h =360;<br /> h /= 60;<br /> i = Math.floor(h);<br /> f = h-i;<br /> p1 = v*(1-s);<br /> p2 = v*(1-s*f);<br /> p3 = v*(1-s*(1-f));<br /> if(i == 0){r=v; g=p3; b=p1;}<br /> else if(i == 1){r=p2; g=v; b=p1;}<br /> else if(i == 2){r=p1; g=v; b=p3;}<br /> else if(i == 3){r=p1; g=p2; b=v;}<br /> else if(i == 4){r=p3; g=p1; b=v;}<br /> else if(i == 5){r=v; g=p1; b=p2;}<br /> kkk3.value='RGB(' Math.round(r*255) ',' Math.round(g*255) ',' Math.round(b*255) ')';<br /> RGBtoHTML(Math.round(r*255),Math.round(g*255),Math.round(b*255))<br /> }</p> <p>//Calculate HTML color code. <br /> function RGBtoHTML(r,g,b){<br /> r=(r>=16)?r.toString(16):('0' r.toString(16))<br> g=(g>=16)?g.toString(16):('0' g.toString(16))<br> b=(b>=16)?b.toString(16):('0' b.toString(16))<br> kkk4.value='HTML #' r g b;<br> }</p> <p>function window.onload(){<br> var RainBow = new Array(255,0,0, 255,255,0, 0,255,0, 0,255,255, 0,0,255, 255,0,255, 255,0,0);<br> for(var i=0;i<6;i ){<br /> var R1 = RainBow[i*3];<br /> var G1 = RainBow[i*3 1];<br /> var B1 = RainBow[i*3 2];<br /> var R2 = RainBow[(i 1)*3];<br /> var G2 = RainBow[(i 1)*3 1];<br /> var B2 = RainBow[(i 1)*3 2];<br /> RainBowDiv.innerHTML = "<div style='position:absolute;left:" i*iW ";top:0;width:" iW ";height:" iH ";background:rgb(" R1 "," G1 " ," B1 ");'></div><div style='position:absolute;left:" i*iW ";top:0;width:" iW ";height:" iH ";background: rgb(" R2 "," G2 "," B2 ");filter:alpha(opacity=0,finishopacity=100,Style=1);'></div>"<br> }<br> RainBowDiv.innerHTML = "<div style=position:absolute;left:0;top:0;width:" 6*iW ";height:" iH ";background:rgb(128,128,128);filter:alpha(opacity=0 ,finishOpacity=100,style=1,starty=0,finishy=100,startx=0,finishx=0)></div><div style='position:absolute;left:0;top:0; width:" (6*iW 1) ";height:" iH ";' onmousemove=HSV()></div>"<br> }<br> </script>

For more js color operations, please refer to the color tool on this site:

RGB Color Coding Generator

Online web color matching tool

RGB color query comparison table_color code table_complete English name of colors

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn