简体   繁体   English

显示选择的随机颜色的十六进制

[英]display hex of random colour selected

I'm building a random background colour picker just for fun. 我正在建立一个随机的背景颜色选择器,只是为了好玩。

Very simply.. as you click a button the page refreshes and a background colour is selected from an array of a few different colours. 非常简单..当您单击按钮时,页面将刷新,并从几种不同颜色的阵列中选择背景颜色。

Now what I'd like to do is I'd like to display the hex of the current colour in plain html and I'm really new to JS so I have no clue how to do this. 现在,我想用纯HTML显示当前颜色的十六进制,而且我对JS真的很陌生,所以我不知道该怎么做。 Could anyone help me out please? 有人可以帮我吗?

Here's my code: 这是我的代码:

<script>
   var bgcolorlist=new Array("#F70000","#B9264F","#990099","#74138C","#0000CE","#1F88A7","#4A9586","#FF2626","#D73E68","#B300B3","#8D18AB","#5B5BFF","#25A0C5","#5EAE9E","#FF5353","#DD597D","#CA00CA","#A41CC6","#7373FF","#29AFD6","#74BAAC","#FF7373","#E37795","#D900D9","#BA21E0","#8282FF","#4FBDDD","#8DC7BB","#FF8E8E","#E994AB","#FF2DFF","#CB59E8","#9191FF","#67C7E2","#A5D3CA","#FFA4A4","#EDA9BC","#F206FF","#CB59E8","#A8A8FF","#8ED6EA","#C0E0DA","#FFB5B5","#F0B9C8","#FF7DFF","#D881ED","#B7B7FF","#A6DEEE","#CFE7E2","#FFC8C8","#F4CAD6","#FFA8FF","#EFCDF8","#C6C6FF","#C0E7F3","#DCEDEA","#FFEAEA","#F8DAE2","#FFC4FF","#EFCDF8","#DBDBFF","#D8F0F8","#E7F3F1")
   document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]
</script>

<form><input type=button value="click here forever" onClick="window.location.reload()"></form> 
<center>
<p>This colour is:  </p>
</div>

Add a span with a given id and set its content to the selected color like this: 添加具有给定id的跨度,并将其内容设置为所选颜色,如下所示:

<script>
    var bgcolorlist=new Array("#F70000","#B9264F","#990099","#74138C","#0000CE","#1F88A7","#4A9586","#FF2626","#D73E68","#B300B3","#8D18AB","#5B5BFF","#25A0C5","#5EAE9E","#FF5353","#DD597D","#CA00CA","#A41CC6","#7373FF","#29AFD6","#74BAAC","#FF7373","#E37795","#D900D9","#BA21E0","#8282FF","#4FBDDD","#8DC7BB","#FF8E8E","#E994AB","#FF2DFF","#CB59E8","#9191FF","#67C7E2","#A5D3CA","#FFA4A4","#EDA9BC","#F206FF","#CB59E8","#A8A8FF","#8ED6EA","#C0E0DA","#FFB5B5","#F0B9C8","#FF7DFF","#D881ED","#B7B7FF","#A6DEEE","#CFE7E2","#FFC8C8","#F4CAD6","#FFA8FF","#EFCDF8","#C6C6FF","#C0E7F3","#DCEDEA","#FFEAEA","#F8DAE2","#FFC4FF","#EFCDF8","#DBDBFF","#D8F0F8","#E7F3F1");
    var randomColor = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
    document.body.style.background = randomColor;
</script>

<form><input type=button value="click here forever" onClick="window.location.reload()"></form> 

<center>
<p>This colour is:  <span id='color'></span></p>

<script>
    document.getElementById("color").innerHTML = randomColor; // Writes the color to the span            
</script>

See demo here: http://jsfiddle.net/2LQNh/ 在此处查看演示: http : //jsfiddle.net/2LQNh/

您可以将值Math.floor(Math.random()*bgcolorlist.length)在变量中,当需要显示当前十六进制时,请使用此变量。

used @RononDex solution and upgraded it with a 使用@RononDex解决方案并将其升级为

Function()

and no reload. 而且没有重新加载

see here http://jsfiddle.net/MGzCE/ 看到这里http://jsfiddle.net/MGzCE/

Try this 尝试这个

HTML HTML

<p>This colour is:  <b id="resu"></b></p>

In Jquery 在jQuery中

var image = new Array ("#F70000","#B9264F","#990099","#74138C","#0000CE","#1F88A7","#4A9586","#FF2626","#D73E68","#B300B3","#8D18AB","#5B5BFF","#25A0C5","#5EAE9E","#FF5353","#DD597D","#CA00CA","#A41CC6","#7373FF","#29AFD6","#74BAAC","#FF7373","#E37795","#D900D9","#BA21E0","#8282FF","#4FBDDD","#8DC7BB","#FF8E8E","#E994AB","#FF2DFF","#CB59E8","#9191FF","#67C7E2","#A5D3CA","#FFA4A4","#EDA9BC","#F206FF","#CB59E8","#A8A8FF","#8ED6EA","#C0E0DA","#FFB5B5","#F0B9C8","#FF7DFF","#D881ED","#B7B7FF","#A6DEEE","#CFE7E2","#FFC8C8","#F4CAD6","#FFA8FF","#EFCDF8","#C6C6FF","#C0E7F3","#DCEDEA","#FFEAEA","#F8DAE2","#FFC4FF","#EFCDF8","#DBDBFF","#D8F0F8","#E7F3F1");
var size = image.length
var x = Math.floor(size*Math.random())

$('body').css('background-color',image[x]);
$('#resu').text(image[x]);

In Javascript 用JavaScript

var bgcolorlist=new Array("#F70000","#B9264F","#990099","#74138C","#0000CE","#1F88A7","#4A9586","#FF2626","#D73E68","#B300B3","#8D18AB","#5B5BFF","#25A0C5","#5EAE9E","#FF5353","#DD597D","#CA00CA","#A41CC6","#7373FF","#29AFD6","#74BAAC","#FF7373","#E37795","#D900D9","#BA21E0","#8282FF","#4FBDDD","#8DC7BB","#FF8E8E","#E994AB","#FF2DFF","#CB59E8","#9191FF","#67C7E2","#A5D3CA","#FFA4A4","#EDA9BC","#F206FF","#CB59E8","#A8A8FF","#8ED6EA","#C0E0DA","#FFB5B5","#F0B9C8","#FF7DFF","#D881ED","#B7B7FF","#A6DEEE","#CFE7E2","#FFC8C8","#F4CAD6","#FFA8FF","#EFCDF8","#C6C6FF","#C0E7F3","#DCEDEA","#FFEAEA","#F8DAE2","#FFC4FF","#EFCDF8","#DBDBFF","#D8F0F8","#E7F3F1")
var col = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
document.body.style.background = col;
document.getElementById("resu").innerHTML = col;

First you need to get the color of the body, this you can achieve by: 首先,您需要获取身体的颜色,这可以通过以下方法实现:

var bgColor = window.getComputedStyle(document.body,null).getPropertyValue('background-color');

which returns you a string like "rgb(170,170,170)" 它会返回一个字符串,例如“ rgb(170,170,170)”

Then you need to parse and convert this String: 然后,您需要解析并转换此String:

//regular expression which returns an array with your 3 decimal values for rgb
var res = bgColor.toString().match(/[0-9]{3}/g);

//this converts a decimal to hex
function componentToHex(c) {
    var hex = parseInt(c).toString(16);  
    return (hex.length < 2) ? "0" + hex : hex;  
}

//this concatenates the new string
function rgbToHex(a, b, c) {
    return "#" + componentToHex(a) + componentToHex(b) + componentToHex(c);
}

//Then you can call it by
var x = rgbToHex(res[0],res[1],res[2]);

This x you can then append to your Span after you gave it an id and got it with document.getElementById() 在给它一个ID并使用document.getElementById()获得它之后,可以将该x追加到Span上

And here an Example Fiddle 这是一个小提琴例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM