nice1 Posted July 3, 2011 Report Share Posted July 3, 2011 tātad uzdrukāju nelielu funkciju kas uzveido vienkāršu krāsas kodu (formātā #123456). tā kā sākotnējā versija bija apm 20-25 rindiņu garumā tad ķēros pie rindiņu skaita samazināšanas.. kad šķiet kad esmu sasniedzis pilnību rodas jautājums vai kāds profiņš nevar pastāstīt vai tomēr nevar vēl īsāk.. :) function randColor(){ $randColor = '#'; for($i = 1;$i<=6;$i++){ $randColor .= dechex(mt_rand(0, 15)); } return $randColor; } Quote Link to comment Share on other sites More sharing options...
daGrevis Posted July 3, 2011 Report Share Posted July 3, 2011 Izlaboju to, ka mainīgais nebija definēts. function generate_color() { $color = ''; for ($i = 0; $i < 6; ++$i) { $color .= dechex(mt_rand(0, 15)); } return '#' . $color; } Šis laikam ir īsākais, ko var: function r(){$r='';for($i=0;$i<6;++$i){$r.=dechex(rand(0,15));}return'#'.$r;} Bet vai vajag? Quote Link to comment Share on other sites More sharing options...
Rincewind Posted July 3, 2011 Report Share Posted July 3, 2011 (edited) function randColor(){ return '#'.dechex(rand(0,16777215)); } Edited July 3, 2011 by Rincewind Quote Link to comment Share on other sites More sharing options...
nice1 Posted July 3, 2011 Author Report Share Posted July 3, 2011 mainīgais ir definēts.. tā kā kļūdiņa tur nepavisam nebija.. :) $randColor = '#'; starpība jau īsti nebūtu vai definējot liekam # zīmīti vai pie return.. vai tomēr? :D Quote Link to comment Share on other sites More sharing options...
codez Posted July 3, 2011 Report Share Posted July 3, 2011 function generate_color(){ return '#'.str_pad(dechex(mt_rand(0,16777215)),6,"0",0); } Quote Link to comment Share on other sites More sharing options...
codez Posted July 3, 2011 Report Share Posted July 3, 2011 (edited) function randColor(){ return '#'.dechex(rand(0,16777215)); } Šijā variantā tiek izvadītas invalīdas krāsa, teiksim ar 4 hexdecimāliem cipariem, pie tam uz windows maksimālais rand skaitlis ir 32768. Edited July 3, 2011 by codez Quote Link to comment Share on other sites More sharing options...
codez Posted July 3, 2011 Report Share Posted July 3, 2011 Šis laikam ir īsākais, ko var: function r(){$r='';for($i=0;$i<6;++$i){$r.=dechex(rand(0,15));}return'#'.$r;} Bet vai vajag? Mans īsais variants - nedaudz, bet tomēr īsāks. function f(){return'#'.str_pad(dechex(mt_rand(0,16777215)),6,"0",0);} Kurš var vēl īsāk? Quote Link to comment Share on other sites More sharing options...
daGrevis Posted July 3, 2011 Report Share Posted July 3, 2011 Nice1, Mana kļūda... pats uztaisīju un tad Tev "braucu virsū"! Hāāh. Pagaidām codez vinē... bet nopietni: Kam Tev vajag to īsumu? Tas samaitā lasāmību. Quote Link to comment Share on other sites More sharing options...
nice1 Posted July 3, 2011 Author Report Share Posted July 3, 2011 (edited) vnk azartā iegāju :) nu jā codez visu cieņu.. Edited July 3, 2011 by nice1 Quote Link to comment Share on other sites More sharing options...
codez Posted July 3, 2011 Report Share Posted July 3, 2011 (edited) Lūk vēl īsāks variants: function f(){return sprintf("#%06X",mt_rand(0,16777215));} Edited July 3, 2011 by codez Quote Link to comment Share on other sites More sharing options...
daGrevis Posted July 3, 2011 Report Share Posted July 3, 2011 Mans īsais variants - nedaudz, bet tomēr īsāks. function f(){return'#'.str_pad(dechex(mt_rand(0,16777215)),6,"0",0);} Kurš var vēl īsāk? function f(){return'#'.str_pad(dechex(rand(0,0xFFFFFF)),6);} 16777215 => 0xFFFFFF ...laikam neko nedeva. Quote Link to comment Share on other sites More sharing options...
daGrevis Posted July 3, 2011 Report Share Posted July 3, 2011 Nē, codez uzvar! Cienu. Quote Link to comment Share on other sites More sharing options...
Rincewind Posted July 3, 2011 Report Share Posted July 3, 2011 (edited) Lūk vēl īsāks variants: function f(){return sprintf("#%06X", mt_rand(0, 16777215));} function f(){ return sprintf("#%06X", md5(rand())); } Nē, pag. Tā neies. Edited July 3, 2011 by Rincewind Quote Link to comment Share on other sites More sharing options...
marrtins Posted July 3, 2011 Report Share Posted July 3, 2011 Laba ideja par to hešu: return '#'.substr(md5(rand()), 0, 6); Quote Link to comment Share on other sites More sharing options...
Rincewind Posted July 3, 2011 Report Share Posted July 3, 2011 (edited) Laba ideja par to hešu: return '#'.substr(md5(rand()), 0, 6); Es apskatījos ka rand() izvada tikai 15 bitīgas vērtības, jāaizvieto uz mt_rand kuram ir 32 biti. Un substr parametrus manuprāt var aizvietot ar -6. return '#'.substr(md5(mt_rand()),-6); Vai pat vel īsāk: return '#'.substr(uniqid(),-6); Edited July 3, 2011 by Rincewind Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.