Jump to content

Color grid


James0231

Recommended Posts

Hey guys I'm brand new to php and I'm trying to make script that will generate a 10x10 grid of randomly generated colors that display the RGB color code inside the grid in both white and black. So far I have a 3x3 grid of the HTML that the PHP script would have to generate in order to accomplish the grid but I'm not sure about how to do the whole thing. Any help? below it's what I have so far (it's all the same color though)

<!DOCTYPE html>
<html>
    <head>
	</head>
	<body>
        <table>
			<tbody>
				<tr>
					<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
					<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
					<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
				</tr>
				<tr>
					<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
					<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
					<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
					</tr>
				<tr>
						<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
						<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
						<td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td>
					</tr>
			</tbody>
		</table>
	</body>
</html>
Link to comment
Share on other sites

Here is one way of accomplishing the task. You don't need the two arrays as you could just have an array of 2 values and randomly select a 0 or 1 for each square. 

<!doctype html>
<html>
<head>

<style type="text/css">

.white {

width:50px;
background-color: #ffffff;

}

.black {

width:50px;
height:50px;
background-color: #000000;

}

</style>
</head>
<body>
<table border=1>

<?php

$white = array_fill(0,10,'white');
$black = array_fill(0,10,'black');
$colors = array_merge($white,$black);

shuffle($colors);

//$colors = array('white','black');

for($i = 0; $i < 10; $i++)
{
    echo "<tr>";

    for($j = 0; $j < 10; $j++)
    {
        echo "<td class='" . $colors[rand(0,19)] . "'></td>";
    }

    echo "</tr>";
}
?>
</table>
</body>
Edited by hansford
Link to comment
Share on other sites

Thank you. Also, I think you misunderstood the part about the RGB black and white colors. The colors of the grid can be random (many different colors) while the RGB color code is to be displayed inside the grid in both white and black. I'm having trouble with that part.  What I'm trying to accomplish is something like this but in a 10x10 grid (see attachment) 121akud.jpg

Edited by James0231
Link to comment
Share on other sites

Make changes as needed, but this will give you what you indicated.

<!doctype html>
<html>
<head>

<style type="text/css">

.white-text {

display:block;
color: #ffffff;
}

.black-text {

display:block;
color: #000000;
}

.white {

width:50px;
height:50px;
background-color: #ffffff;

}

.black {

width:50px;
height:50px;
background-color: #173534;

}

.green {

width:50px;
height:50px;
background-color: #247422;

}

.pee-green {

width:50px;
height:50px;
background-color: #A3BC34;

}

.gray {

width:50px;
height:50px;
background-color: #AABBCC;

}
.dark-blue  {

width:50px;
height:50px;
background-color: #1351AC;

}
.lt-green {

width:50px;
height:50px;
background-color: #547142;

}
.red {

width:50px;
height:50px;
background-color: #AD2431;

}
.teal {

width:50px;
height:50px;
background-color: #113344;

}
.purple {

width:50px;
height:50px;
background-color: #431241;

}

</style>
</head>
<body>
<table border=1>

<?php

$colors = array(array('white','FFFFFF'),array('black','173534'),
array('green','247422'),array('pee-green','A3BC34'),array('gray','AABBCC'),
array('dark-blue','1351AC'),array('lt-green','547142'),array('red','AD2431'),
array('teal','113344'),array('purple','431241'));

for($i = 0; $i < 10; $i++)
{
    echo "<tr>";

    for($j = 0; $j < 10; $j++)
    {
        $rand = rand(0,9);
        echo "<td class='" . $colors[$rand][0] . "'>
        <span class='black-text'>{$colors[$rand][1]}</span>
        <span class='white-text'>{$colors[$rand][1]}</span>
        </td>";
    }

    echo "</tr>";
}
?>
</table>
</body>
</html>
Link to comment
Share on other sites

or

<?php
function randColor() 
{
    $r = rand(0,255);
    $g = rand(0,255);
    $b = rand(0,255);
    
    return sprintf('%02X%02X%02X', $r, $g, $b);
}

$tdata='';
for ($i=0; $i<10; $i++) {
    $tdata .= "<tr>";
    for ($j=0; $j<10; $j++) {
        $c = randColor();
        $tdata .= "<td style='background-color:#$c'>$c<br><span class='wtext'>$c</span></td>";
    }
    $tdata .= '</tr>';
}
?>
<html>
<head>
<style type='text/css'>
table {
    border-collapse: collapse;
}
td {
    width: 60px;
    height: 60px;
    font-family: sans-serif;
    font-size: 8pt;
    text-align: center;
}
.wtext {
    color: white;
}
</style>
</head>
<body>
<table border='1'>
    <?=$tdata?>
</table>
</body>
</html>

example output

post-3105-0-95103300-1443171883_thumb.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.