[{"data":1,"prerenderedAt":738},["ShallowReactive",2],{"i-lucide:menu":3,"i-lucide:arrow-up-right":8,"i-lucide:moon":10,"i-lucide:sun":12,"i-lucide:rss":14,"i-simple-icons:github":16,"i-simple-icons:linkedin":18,"post-\u002F2011\u002Fphp-algorithm-for-consistent-random-colors":21,"surround-\u002F2011\u002Fphp-algorithm-for-consistent-random-colors":725,"i-lucide:arrow-left":734,"i-lucide:arrow-right":736},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 5h16M4 12h16M4 19h16\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M7 7h10v10M7 17L17 7\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":11},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":13},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"12\" cy=\"12\" r=\"4\"\u002F>\u003Cpath d=\"M12 2v2m0 16v2M4.93 4.93l1.41 1.41m11.32 11.32l1.41 1.41M2 12h2m16 0h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M4 11a9 9 0 0 1 9 9M4 4a16 16 0 0 1 16 16\"\u002F>\u003Ccircle cx=\"5\" cy=\"19\" r=\"1\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":17},"\u003Cpath fill=\"currentColor\" d=\"M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":19,"hidden":20},"\u003Cpath fill=\"currentColor\" d=\"M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037c-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85c3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.06 2.06 0 0 1-2.063-2.065a2.064 2.064 0 1 1 2.063 2.065m1.782 13.019H3.555V9h3.564zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0z\"\u002F>",true,{"id":22,"title":23,"body":24,"category":710,"comments":711,"date":712,"description":30,"excerpt":711,"extension":713,"image":711,"meta":714,"navigation":20,"path":715,"readingTime":716,"seo":717,"stem":718,"subtitle":711,"tags":719,"wordCount":723,"__hash__":724},"posts\u002F2011\u002Fphp-algorithm-for-consistent-random-colors.md","PHP algorithm for consistent random colors",{"type":25,"value":26,"toc":708},"minimark",[27,31,34,704],[28,29,30],"p",{},"Here's a PHP function that returns 2 random, but (mostly) constant HEX color codes.",[28,32,33],{},"The function takes a single parameter - a string - and returns an array with 2 colors.",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-php shiki shiki-themes github-light github-dark","    \u002F**\n     * A function that returns a random background and font color.\n     * The output is constant as long as the input string remains unchanged.\n     * @author Ando Roots\n     * @date August 2011\n     * @copyright Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License\n     * @link http:\u002F\u002Fsqroot.eu\n     * @static\n     * @param string $string A random input string that determines the output colors.\n     * @return array 2 colors, index 0 being random background color, index 1 gray font color,\n     * it's darkness dependant on the first color.\n     * Example: array(2) { [0]=> string(7) \"#62FFD5\" [1]=> string(6) \"#9D02A\" }\n     *\u002F\n    public static function color($string = 'test') {\n        $colors = array();\n        $color = array(0, 0, 0);\n\n        \u002F\u002F Prepare the input string.\n        $string = preg_replace('\u002F[^0-9]\u002F', '', md5($string));\n\n        \u002F\u002F Determine it's length\n        if (strlen($string) > 8) {\n            $length = 3;\n        } elseif (strlen($string) > 5) {\n            $length = 2;\n        } elseif (strlen($string) > 2) {\n            $length = 1;\n        } else {\n            \u002F\u002F Use time if all else fails\n            $length = 3;\n            $string = strrev(time());\n        }\n\n        \u002F\u002F Generate background color\n        $hex = '#';\n        foreach ($color as $i => $c) {\n            $color[$i] = (int)substr($string, $i * $length, $length);\n            if ($color[$i] > 255) {\n                $color[$i] = 255;\n            }\n            $hex .= dechex($color[$i]);\n        }\n        $colors[] = strtoupper($hex);\n\n        \u002F\u002F Generate font color\n        $hex = '#';\n        $darkness = abs(255- round(array_sum($color) \u002F 3));\n        $hex .= dechex($darkness).dechex($darkness).dechex($darkness);\n        $colors[] = strtoupper($hex);\n\n        return $colors;\n    }\n","php","",[42,43,44,53,59,65,78,84,95,106,114,128,142,148,154,160,190,204,232,238,244,280,285,291,314,328,349,361,380,392,403,409,420,439,445,450,456,469,490,518,534,546,552,567,572,586,591,597,608,646,673,684,689,698],"code",{"__ignoreMap":40},[45,46,49],"span",{"class":47,"line":48},"line",1,[45,50,52],{"class":51},"sJ8bj","    \u002F**\n",[45,54,56],{"class":47,"line":55},2,[45,57,58],{"class":51},"     * A function that returns a random background and font color.\n",[45,60,62],{"class":47,"line":61},3,[45,63,64],{"class":51},"     * The output is constant as long as the input string remains unchanged.\n",[45,66,68,71,75],{"class":47,"line":67},4,[45,69,70],{"class":51},"     * ",[45,72,74],{"class":73},"szBVR","@author",[45,76,77],{"class":51}," Ando Roots\n",[45,79,81],{"class":47,"line":80},5,[45,82,83],{"class":51},"     * @date August 2011\n",[45,85,87,89,92],{"class":47,"line":86},6,[45,88,70],{"class":51},[45,90,91],{"class":73},"@copyright",[45,93,94],{"class":51}," Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License\n",[45,96,98,100,103],{"class":47,"line":97},7,[45,99,70],{"class":51},[45,101,102],{"class":73},"@link",[45,104,105],{"class":51}," http:\u002F\u002Fsqroot.eu\n",[45,107,109,111],{"class":47,"line":108},8,[45,110,70],{"class":51},[45,112,113],{"class":73},"@static\n",[45,115,117,119,122,125],{"class":47,"line":116},9,[45,118,70],{"class":51},[45,120,121],{"class":73},"@param",[45,123,124],{"class":73}," string",[45,126,127],{"class":51}," $string A random input string that determines the output colors.\n",[45,129,131,133,136,139],{"class":47,"line":130},10,[45,132,70],{"class":51},[45,134,135],{"class":73},"@return",[45,137,138],{"class":73}," array",[45,140,141],{"class":51}," 2 colors, index 0 being random background color, index 1 gray font color,\n",[45,143,145],{"class":47,"line":144},11,[45,146,147],{"class":51},"     * it's darkness dependant on the first color.\n",[45,149,151],{"class":47,"line":150},12,[45,152,153],{"class":51},"     * Example: array(2) { [0]=> string(7) \"#62FFD5\" [1]=> string(6) \"#9D02A\" }\n",[45,155,157],{"class":47,"line":156},13,[45,158,159],{"class":51},"     *\u002F\n",[45,161,163,166,169,172,176,180,183,187],{"class":47,"line":162},14,[45,164,165],{"class":73},"    public",[45,167,168],{"class":73}," static",[45,170,171],{"class":73}," function",[45,173,175],{"class":174},"sScJk"," color",[45,177,179],{"class":178},"sVt8B","($string ",[45,181,182],{"class":73},"=",[45,184,186],{"class":185},"sZZnC"," 'test'",[45,188,189],{"class":178},") {\n",[45,191,193,196,198,201],{"class":47,"line":192},15,[45,194,195],{"class":178},"        $colors ",[45,197,182],{"class":73},[45,199,138],{"class":200},"sj4cs",[45,202,203],{"class":178},"();\n",[45,205,207,210,212,214,217,220,223,225,227,229],{"class":47,"line":206},16,[45,208,209],{"class":178},"        $color ",[45,211,182],{"class":73},[45,213,138],{"class":200},[45,215,216],{"class":178},"(",[45,218,219],{"class":200},"0",[45,221,222],{"class":178},", ",[45,224,219],{"class":200},[45,226,222],{"class":178},[45,228,219],{"class":200},[45,230,231],{"class":178},");\n",[45,233,235],{"class":47,"line":234},17,[45,236,237],{"emptyLinePlaceholder":20},"\n",[45,239,241],{"class":47,"line":240},18,[45,242,243],{"class":51},"        \u002F\u002F Prepare the input string.\n",[45,245,247,250,252,255,257,260,264,267,269,272,274,277],{"class":47,"line":246},19,[45,248,249],{"class":178},"        $string ",[45,251,182],{"class":73},[45,253,254],{"class":200}," preg_replace",[45,256,216],{"class":178},[45,258,259],{"class":185},"'\u002F",[45,261,263],{"class":262},"sA_wV","[^0-9]",[45,265,266],{"class":185},"\u002F'",[45,268,222],{"class":178},[45,270,271],{"class":185},"''",[45,273,222],{"class":178},[45,275,276],{"class":200},"md5",[45,278,279],{"class":178},"($string));\n",[45,281,283],{"class":47,"line":282},20,[45,284,237],{"emptyLinePlaceholder":20},[45,286,288],{"class":47,"line":287},21,[45,289,290],{"class":51},"        \u002F\u002F Determine it's length\n",[45,292,294,297,300,303,306,309,312],{"class":47,"line":293},22,[45,295,296],{"class":73},"        if",[45,298,299],{"class":178}," (",[45,301,302],{"class":200},"strlen",[45,304,305],{"class":178},"($string) ",[45,307,308],{"class":73},">",[45,310,311],{"class":200}," 8",[45,313,189],{"class":178},[45,315,317,320,322,325],{"class":47,"line":316},23,[45,318,319],{"class":178},"            $length ",[45,321,182],{"class":73},[45,323,324],{"class":200}," 3",[45,326,327],{"class":178},";\n",[45,329,330,333,336,338,340,342,344,347],{"class":47,"line":5},[45,331,332],{"class":178},"        } ",[45,334,335],{"class":73},"elseif",[45,337,299],{"class":178},[45,339,302],{"class":200},[45,341,305],{"class":178},[45,343,308],{"class":73},[45,345,346],{"class":200}," 5",[45,348,189],{"class":178},[45,350,352,354,356,359],{"class":47,"line":351},25,[45,353,319],{"class":178},[45,355,182],{"class":73},[45,357,358],{"class":200}," 2",[45,360,327],{"class":178},[45,362,364,366,368,370,372,374,376,378],{"class":47,"line":363},26,[45,365,332],{"class":178},[45,367,335],{"class":73},[45,369,299],{"class":178},[45,371,302],{"class":200},[45,373,305],{"class":178},[45,375,308],{"class":73},[45,377,358],{"class":200},[45,379,189],{"class":178},[45,381,383,385,387,390],{"class":47,"line":382},27,[45,384,319],{"class":178},[45,386,182],{"class":73},[45,388,389],{"class":200}," 1",[45,391,327],{"class":178},[45,393,395,397,400],{"class":47,"line":394},28,[45,396,332],{"class":178},[45,398,399],{"class":73},"else",[45,401,402],{"class":178}," {\n",[45,404,406],{"class":47,"line":405},29,[45,407,408],{"class":51},"            \u002F\u002F Use time if all else fails\n",[45,410,412,414,416,418],{"class":47,"line":411},30,[45,413,319],{"class":178},[45,415,182],{"class":73},[45,417,324],{"class":200},[45,419,327],{"class":178},[45,421,423,426,428,431,433,436],{"class":47,"line":422},31,[45,424,425],{"class":178},"            $string ",[45,427,182],{"class":73},[45,429,430],{"class":200}," strrev",[45,432,216],{"class":178},[45,434,435],{"class":200},"time",[45,437,438],{"class":178},"());\n",[45,440,442],{"class":47,"line":441},32,[45,443,444],{"class":178},"        }\n",[45,446,448],{"class":47,"line":447},33,[45,449,237],{"emptyLinePlaceholder":20},[45,451,453],{"class":47,"line":452},34,[45,454,455],{"class":51},"        \u002F\u002F Generate background color\n",[45,457,459,462,464,467],{"class":47,"line":458},35,[45,460,461],{"class":178},"        $hex ",[45,463,182],{"class":73},[45,465,466],{"class":185}," '#'",[45,468,327],{"class":178},[45,470,472,475,478,481,484,487],{"class":47,"line":471},36,[45,473,474],{"class":73},"        foreach",[45,476,477],{"class":178}," ($color ",[45,479,480],{"class":73},"as",[45,482,483],{"class":178}," $i ",[45,485,486],{"class":73},"=>",[45,488,489],{"class":178}," $c) {\n",[45,491,493,496,498,500,503,506,509,512,515],{"class":47,"line":492},37,[45,494,495],{"class":178},"            $color[$i] ",[45,497,182],{"class":73},[45,499,299],{"class":178},[45,501,502],{"class":73},"int",[45,504,505],{"class":178},")",[45,507,508],{"class":200},"substr",[45,510,511],{"class":178},"($string, $i ",[45,513,514],{"class":73},"*",[45,516,517],{"class":178}," $length, $length);\n",[45,519,521,524,527,529,532],{"class":47,"line":520},38,[45,522,523],{"class":73},"            if",[45,525,526],{"class":178}," ($color[$i] ",[45,528,308],{"class":73},[45,530,531],{"class":200}," 255",[45,533,189],{"class":178},[45,535,537,540,542,544],{"class":47,"line":536},39,[45,538,539],{"class":178},"                $color[$i] ",[45,541,182],{"class":73},[45,543,531],{"class":200},[45,545,327],{"class":178},[45,547,549],{"class":47,"line":548},40,[45,550,551],{"class":178},"            }\n",[45,553,555,558,561,564],{"class":47,"line":554},41,[45,556,557],{"class":178},"            $hex ",[45,559,560],{"class":73},".=",[45,562,563],{"class":200}," dechex",[45,565,566],{"class":178},"($color[$i]);\n",[45,568,570],{"class":47,"line":569},42,[45,571,444],{"class":178},[45,573,575,578,580,583],{"class":47,"line":574},43,[45,576,577],{"class":178},"        $colors[] ",[45,579,182],{"class":73},[45,581,582],{"class":200}," strtoupper",[45,584,585],{"class":178},"($hex);\n",[45,587,589],{"class":47,"line":588},44,[45,590,237],{"emptyLinePlaceholder":20},[45,592,594],{"class":47,"line":593},45,[45,595,596],{"class":51},"        \u002F\u002F Generate font color\n",[45,598,600,602,604,606],{"class":47,"line":599},46,[45,601,461],{"class":178},[45,603,182],{"class":73},[45,605,466],{"class":185},[45,607,327],{"class":178},[45,609,611,614,616,619,621,624,627,630,632,635,638,641,643],{"class":47,"line":610},47,[45,612,613],{"class":178},"        $darkness ",[45,615,182],{"class":73},[45,617,618],{"class":200}," abs",[45,620,216],{"class":178},[45,622,623],{"class":200},"255",[45,625,626],{"class":73},"-",[45,628,629],{"class":200}," round",[45,631,216],{"class":178},[45,633,634],{"class":200},"array_sum",[45,636,637],{"class":178},"($color) ",[45,639,640],{"class":73},"\u002F",[45,642,324],{"class":200},[45,644,645],{"class":178},"));\n",[45,647,649,651,653,655,658,661,664,666,668,670],{"class":47,"line":648},48,[45,650,461],{"class":178},[45,652,560],{"class":73},[45,654,563],{"class":200},[45,656,657],{"class":178},"($darkness)",[45,659,660],{"class":73},".",[45,662,663],{"class":200},"dechex",[45,665,657],{"class":178},[45,667,660],{"class":73},[45,669,663],{"class":200},[45,671,672],{"class":178},"($darkness);\n",[45,674,676,678,680,682],{"class":47,"line":675},49,[45,677,577],{"class":178},[45,679,182],{"class":73},[45,681,582],{"class":200},[45,683,585],{"class":178},[45,685,687],{"class":47,"line":686},50,[45,688,237],{"emptyLinePlaceholder":20},[45,690,692,695],{"class":47,"line":691},51,[45,693,694],{"class":73},"        return",[45,696,697],{"class":178}," $colors;\n",[45,699,701],{"class":47,"line":700},52,[45,702,703],{"class":178},"    }\n",[705,706,707],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sA_wV, html code.shiki .sA_wV{--shiki-default:#032F62;--shiki-dark:#DBEDFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":40,"searchDepth":55,"depth":55,"links":709},[],"Learning",null,"2011-08-05","md",{},"\u002F2011\u002Fphp-algorithm-for-consistent-random-colors","about 1 minute",{"title":23,"description":30},"2011\u002Fphp-algorithm-for-consistent-random-colors",[39,42,720,721,722],"function","random color","snippet",259,"YT3RMTAK3dMYOYPv8c5rvXoLEdW0pccLE9V2bh7756I",[726,730],{"title":727,"path":728,"stem":729,"children":-1},"Kohana 3.2 Validation Estonian translation","\u002F2011\u002Fkohana-3-2-validation-estonian-translation","2011\u002Fkohana-3-2-validation-estonian-translation",{"title":731,"path":732,"stem":733,"children":-1},"Time-coded email service","\u002F2011\u002Ftime-coded-email-service","2011\u002Ftime-coded-email-service",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":735},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m12 19l-7-7l7-7m7 7H5\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":737},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M5 12h14m-7-7l7 7l-7 7\"\u002F>",1790886932889]