Toucan Flock
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Random Color Codes

Go down

Random Color Codes Empty Random Color Codes

Post by PTGigi Mon Feb 25, 2013 9:30 am

Copy-pasta of the topic I made on MA because some of the codes I'd like to keep to mess around with XD Probably going to make a few updates to these codes as well

I've made a few random codes and figured I'd share them. Nothing special, and they aren't very practical. But I hope they amuse you for a short while. :3 If you wish to test the code, just paste them here.

Random Colors
Wrote this one awhile ago so it's really long and chunky, but I like it :3 Generates 3 random colors. EDIT: Made less chunky (5/14/12) EDIT: Made super less chunky (2/24/13)
Code:

<script type="text/javascript">
for(i=0;i<3;i++){
fullhex=(Math.floor(Math.random()*16777215)).toString(16);
document.write("<p style=\"color:#", fullhex, "\"> <strong>#", fullhex, "  ██████</strong></p>");
}
</script>

Average between several colors (EDITED 2/24/13)
Finds the average (then the averages of the average O: ) between colors. Randomly generates them, but I can explain how to preadd your own colors. Right at the top where it says i<3, change the 3 to however many numbers you want it to generate.
Code:
<script>
var arr=[];

for(i=0;i<3;i++){
arr[i]=[Math.floor(Math.random()*256),Math.floor(Math.random()*256),Math.floor(Math.random()*256)];
}

for(i=0;i<arr.length;i++){
document.write(spitOut(i));
}
document.write("<br />");

for(j=0;j<5;j++){
for(i=1;i<arr.length;i+=2){
arr.splice(i,0,[avg(arr[i-1][0],arr[i][0]),avg(arr[i-1][1],arr[i][1]),avg(arr[i-1][2],arr[i][2])]);
}
}

for(i=0;i<arr.length;i++){
document.write(spitOut(i));
}

function avg(num1,num2){
x=Math.floor((num1+num2)/2);
return x;
}

function spitOut(i){
str="("+arr[i][0]+","+arr[i][1]+","+arr[i][2]+")",full="<span style='background-color:rgb"+str+"'>&nbsp;</span>";
return full;
}
</script>

Displays a bunch of colors: EDIT: Made less chunky (2/24/13)
Code:
<script>
for(i=0;i<16;i++){
document.write('<button onclick="myFunction('+i+')">'+(i.toString(16).toUpperCase())+'</button>');
}
document.write('<p id="demo"></p>');

function myFunction(green){
var x="<table style='border-collapse:collapse;'><tr>", red=blue=0, green=green;
for(blue=0;blue<16;blue++){
for(red=0;red<16;red++){
x+="<td style='background-color: #"+(red.toString(16))+(blue.toString(16))+(green.toString(16))+"; width:20px; height:20px;'></td>";
}
x+="</tr><tr>";
}
x+="</tr></table>";
document.getElementById("demo").innerHTML=x;
}

</script>

Generates colors with an average brightness (ie not white or black or colors close to those)
Code:
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction()
{
var x="",red=Math.floor(Math.random()*16), blue=Math.floor(Math.random()*16),  green=Math.floor(Math.random()*16), mathy=red+blue+green, hex=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
while (mathy>=30 || mathy<=15){
var red=Math.floor(Math.random()*16),blue=Math.floor(Math.random()*16), green=Math.floor(Math.random()*16),mathy=red+blue+green;
}
x+="<table><tr><td style='background-color: #"+hex[red]+hex[blue]+hex[green]+"; '>"+hex[red]+hex[blue]+hex[green]+"</td></tr></table>";
document.getElementById("demo").innerHTML=x;
}
</script>

</body>
</html>
PTGigi
PTGigi
Administrator
Administrator

Posts : 34
Beak Points : 4333
Reputation : 1
Join date : 2013-02-25
Age : 30
Location : Somewhere o3o

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum