# Php generates random colors with different colors of adjacent elements

Source: Internet
Author: User
Php generates random colors, and the colors of adjacent elements are different. php random

Such as the question, is the layout form
1 2 3
4 5 6
7 8 9
The color of adjacent elements is different. for example, if the value is 5, the color of the element is different from that of the value 1, 3, 7, and 9. how can we generate a random color, but how can we achieve different colors. There are about 6 alternative colors. Thank you.

Clarify the rules

Is the color of adjacent elements 4 and 5 adjacent to each other? If yes, the only six colors cannot be implemented. redescribe your requirements... if not, then you define the concept of "adjacent elements" here.

Clarify the rules

Is the color of adjacent elements 4 and 5 adjacent to each other? If yes, the only six colors cannot be implemented. redescribe your requirements... if not, then you define the concept of "adjacent elements" here.
Well, 4 and 5 are adjacent .. If six cannot be implemented, it can be added.

1 2 3
4 5 1
2 3 6

Does this meet the requirements?

Clarify the rules

Is the color of adjacent elements 4 and 5 adjacent to each other? If yes, the only six colors cannot be implemented. redescribe your requirements... if not, then you define the concept of "adjacent elements" here.
Well, 4 and 5 are adjacent .. If six cannot be implemented, it can be added.

The difference between limited colors and unrestricted colors should be (yes). The former can be calculated based on the finite colors. The latter can be randomly obtained directly and then determined if it cannot be obtained again.

1 2 1
3 4 3
1 2 1

Four-color map?
Weak algorithms

My idea is to traverse from left to right, from top to bottom, and fill in numbers (1 ~ N), as long as the grid adjacent to the front (horizontal and vertical) appears, it will be skipped. if it does not appear, select this number.
For example, in the upper right corner, because there is not the second row, only one Grid 2 on the left is adjacent, so 1 ~ In n, 1 will be met directly, and fill in the upper right corner

1 2 3
4 5 1
2 3 6

Does this meet the requirements?
But this style is loaded through the waterfall stream. The height is not fixed, so it is very tangled. It is possible that the 2, 5, and 3 in the middle are close to 1!

1 2 3
4 5 1
2 3 6

Does this meet the requirements?
The pages use masonry and infinitescroll. each row contains three blocks, with fixed width and fixed height. Then we need to implement different adjacent background colors. I don't know how to implement them?

If #5 also meets the requirements, you can say: the singular row is 1 2 1, the double row is 3 4 3 ...... Loop.

`\$ D = array (300,); \$ a = array (\$ d [rand (0, 5)]); \$ loop =; // used to prevent endless loops while (count (\$ a) <210 & \$ loop --) {\$ p = count (\$ a); \$ n = \$ d [0]; \$ d [] = array_shift (\$ d); switch (\$ p % 3) {case 1: if (\$ a [\$ P-1] ==\$ n) continue; if (isset (\$ a [\$ P-3]) & \$ a [\$ P-3] ==\$ n) continue; \$ a [] = \$ n; break; case 2: if (\$ a [\$ P-1] =\$ n) continue; if (isset (\$ a [\$ P-2]) & \$ a [\$ P-2] ==\$ n) continue; if (isset (\$ a [\$ P-3]) & \$ a [\$ P-3] == N) continue; if (isset (\$ a [\$ P-6]) & \$ a [\$ P-6] =n n) continue; if (isset (\$ a [\$ P-8]) & \$ a [\$ P-8] ==\$ n) continue; \$ a [] = \$ n; break; case 0: if (isset (\$ a [\$ P-3]) & \$ a [\$ P-3] ==\$ n) continue; if (isset (\$ a [\$ P-4]) & \$ a [\$ P-4] =n n) continue; if (isset (\$ a [\$ P-6]) & \$ a [\$ P-6] ==\$ n) continue; \$ a [] = \$ n; break;} // \$ a [] = \$ n; // if it is removed from the case, an error is returned! BUG} \$ t = array_map (function (\$ r) {return join (',', \$ r) ;}, array_chunk (\$ a, 3 )); print_r (\$ t );`
Array
(
[0] => 1, 2, 3
[1] => 4, 5, 6
[2] => 2, 3, 4
[3] => 5, 6, 1
[4] => 3, 4, 5
[5] => 6, 1, 2
[6] => 4, 5, 6
[7] => 1, 2, 3
[8] => 5, 6, 1
[9] => 2, 3, 4
[10] => 6, 1, 2
[11] => 3, 4, 5
[12] => 1, 2, 3
[13] => 4, 5, 6
[14] => 2, 3, 4
[15] => 5, 6, 1
[16] => 3, 4, 5
[17] => 6, 1, 2
[18] => 4, 5, 6
[19] => 1, 2, 3
[20] => 5, 6, 1
[21] => 2, 3, 4
[22] => 6, 1, 2
[23] => 3, 4, 5
[24] => 1, 2, 3
[25] => 4, 5, 6
[26] => 2, 3, 4
[27] => 5, 6, 1
[28] => 3, 4, 5
[29] => 6, 1, 2
[30] => 4, 5, 6
[31] => 1, 2, 3
[32] => 5, 6, 1
[33] => 2, 3, 4
[34] => 6, 1, 2
[35] => 3, 4, 5
[36] => 1, 2, 3
[37] => 4, 5, 6
[38] => 2, 3, 4
[39] => 5, 6, 1
[40] => 3, 4, 5
[41] => 6, 1, 2
[42] => 4, 5, 6
[43] => 1, 2, 3
[44] => 5, 6, 1
[45] => 2, 3, 4
[46] => 6, 1, 2
[47] => 3, 4, 5
[48] => 1, 2, 3
[49] => 4, 5, 6
[50] => 2, 3, 4
[51] => 5, 6, 1
[52] => 3, 4, 5
[53] => 6, 1, 2
[54] => 4, 5, 6
[55] => 1, 2, 3
[56] => 5, 6, 1
[57] => 2, 3, 4
[58] => 6, 1, 2
[59] => 3, 4, 5
[60] => 1, 2, 3
[61] => 4, 5, 6
[62] => 2, 3, 4
[63] => 5, 6, 1
[64] => 3, 4, 5
[65] => 6, 1, 2
[66] => 4, 5, 6
[67] => 1, 2, 3
[68] => 5, 6, 1
[69] => 2, 3, 4
)
Any three rows in a row will meet your needs.

After several days, I finally found a php bug.
Still satisfied

If there are problems with the basic syntax structure, it is time for php to enter the grave!
Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.