Jump to content
php.lv forumi

funckijas noīsināšana


nice1

Recommended Posts

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;
}

Link to comment
Share on other sites

  • Replies 30
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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?

Link to comment
Share on other sites

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 by codez
Link to comment
Share on other sites

Š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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Rincewind
Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...