Change random strings into colours, consistently - php

I have some IDs that I want to generate random colours for. Making random colours is not the issue, but it has to be consistent.
I could MD5 (or some other kind of hash) the IDs so the code could know how much characters to expect, but the bottomline is it has to generate the same (random) colour for the same ID/hash/string.

All you need for a RGB code is a consistent mapping from your random string to a 6 position hex value. Why not use md5 as a way to a hex string, and then take the first 6 digits?
<?php
function rgbcode($id){
return '#'.substr(md5($id), 0, 6);
}
?>

Here's a simple function that I'm using in one of my own projects to create RGB values which I can use with PHP's imagecolorallocatealpha:
function randomRgbGeneratorFromId( $id = null ) {
$md5 = md5( $id );
$md5 = preg_replace( '/[^0-9a-fA-F]/', '', $md5 );
$color = substr( $md5, 0, 6 );
$hex = str_split( $color, 1 );
$rgbd = array_map( 'hexdec', $hex );
$rgba = array(
( $rgbd[0] * $rgbd[1] ),
( $rgbd[2] * $rgbd[3] ),
( $rgbd[4] * $rgbd[5] ),
);
return $rgba;
}
Of course you can always output to your CSS / HTML by using something like this:
echo sprintf( 'rgb(%s)', implode( ',', randomRgbGeneratorFromId( $id ) ) );

first convert random string into hexadecimal number with 6 digits and remove the floats and make the string into integer and convert them into a hexadecimal string and then put a hash before six digit hexadecimal number and there is a color for that random string and you may find hexadecimal number be more than a six digit one , if so , continue dividing that number with 2 till it becomes 5 digit hexadecimal number and the sixth digit will be the number of time the total hexadecimal number divided by 2, there is your color code.

Related

php placement of the dot in a long number

I want to display a large number with a leading zero and a dot after.
The balance i want to display starts with 0.000000000000000000 ( 18 zeros after the dot ). This should be able to go up to 99.00000000000000000.( 17 zeros after the dot ).
I did a lot of trial and error but i just can't seem to get the dot in there. As far as for the zeros i got it working. What i have now is:
$leadingBalance = sprintf("%019d", $balance);
echo $leadingBalance;
This will display the correct balance but i need to place the dot in there. It means that if my balance has 17 or 18 numbers it should place the dot as 0.0000... If the balance has 19 numbers it should place the dot as 00.0000...
Whatever i try, how much i look up i can't figure it out.
For eg:
$n1 = 0;
$n2 = 99;
echo number_format($n1,18)."<br>";
echo number_format($n2,18)."<br>";
See the documentation for number_format: http://php.net/number_format
The functions parameters are:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
So use:
number_format(1000000000000000, 2, '.', '');
Which means that you don't use any (= empty string) thousands separator, only a decimal point.
or if you just want padding of 19 zero after decimal point
just use
sprintf("%0.19f",$number);
or else
if u want a number always 20 digit without caring about whole no and decimal value than use str_pad()
eg:
$no = sprintf("%0.2f",100); //100.00
this will convert your no to decimal point with 2 digit after decimal now just pad some digit if require to make it 20 digit long
echo str_pad($no,20,"0"); //100.00(15 zero after this)
this will check no of digit available and pad 0 to make it 20 digit
for more ref:https://www.w3schools.com/php/func_string_number_format.asp
You are using %019d when you actually wants a float number, try this:
<?php
$format = '%0.19f';
$args = 9;
$result = sprintf ($format, $args);
//$result will be equal to 9.0000000000000000000
?>
<?php
$format = '%0.19f';
$args = 99;
$result = sprintf ($format, $args);
//$result will be equal to 99.0000000000000000000
?>
Edit:
Since it looks like you want your whole number to be equal to 20 digits only, you may try to do some math if number_format doesn't do your job. You can try something like:
<?php
$number = 999;
$number_length = strlen($number);
$format_len = 20 - $number_length;
$format = '%0.'. $format_len .'f';
$result = sprintf($format, $number);
?>

Padding with %03.3f doesn't work

I have the following function (in PHP):
function FormatSize( $size ) {
$size = intval( $size );
$idx = 0;
$prefixes = array( "", "Ki", "Mi", "Gi", "Ti", "Pi" );
while( $size > 1024 ) {
$size = $size / 1024;
$idx++;
}
return sprintf( "%03.2f %sB", $size, $prefixes[$idx] );
}
and the following in lua
function GetSize( iSize )
local iSizeTemp, sPrefix, i = tonumber(iSize) or 0, {"", "Ki", "Mi", "Gi", "Ti", "Pi"}, 1
while iSizeTemp > 1024 do
iSizeTemp, i = iSizeTemp/1024, i+1
end
return string.format( "%03.2f %sB", iSizeTemp, sPrefix[i] )
end
The return statement uses %03.2f which should return the output as PQR.XY format, where P or Q or both can be zero.
But I'm getting a simple output (the same as %.2f) and the padding part (%03) seems to have no effect whatsoever. Here is the codepad link (for PHP and for Lua) for an example; where I'm passing a number 1285854482 to the function. The output comes to be 1.20 GiB instead of 001.20 GiB.
You seem to misundestand the description of %.f format specification: this - %03.2f - actually means 'allocate at least 3 symbols to represent a number, at least 2 of them for representing its non-integer part'. Note that full stop is not included into this 2 - but is included into 3, so this formatter never actually hits its 'minimum'. )
Change it into '%06.2f', and it'll work as you expect: of 6 symbols, 3 will be spent on a full stop symbol and two digits after, so 3 remain for representing the integer part of the number.
Ah. Found the solution after going through C++ reference on printf, which states about the number succeeding % sign as:
Minimum number of characters to be printed. If the value to be printed
is shorter than this number, the result is padded with blank spaces.
The value is not truncated even if the result is larger.

Convert 64 bit Integer hex string to 64 bit decimal string on 32 bit system

What is a simple way to convert a 64 bit integer encoded as a hex string to a decimal string on a 32 bit system. It needs to be the full value, it can not be in scientific notation or truncated :/
"0c80000000000063" == "900719925474099299"
"0c80000000000063" != 9.007199254741E+17
PHP's base_convert() and hexdec() don't do the job right.
You need to use BC Math PHP extension (bundled).
First split your input string to get high and low bytes, next convert it to decimal and then do calculation via BC functions like this:
$input = "0C80000000000063";
$str_high = substr($input, 0, 8);
$str_low = substr($input, 8, 8);
$dec_high = hexdec($str_high);
$dec_low = hexdec($str_low);
//workaround for argument 0x100000000
$temp = bcmul ($dec_high, 0xffffffff);
$temp2 = bcadd ($temp, $dec_high);
$result = bcadd ($temp2, $dec_low);
echo $result;
/*
900719925474099299
*/
Have you seen the first comment to hexdec's help page on php.net?
When given large numbers, the hexdec function automatically converts
the value to scientific notation. So, "aa1233123124121241" as a
hexadecimal value will be converted to "3.13725790445E+21". If you're
converting a hexadecimal value that represents a hash value (md5 or
sha), then you need every single bit of that representation to make it
useful. By using the number_format function, you can do that
perfectly. For example :
<?php
// Author: holdoffhunger#gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_string = "1234567890abcdef1234567890abcdef";
// Converted to Decimal
// ---------------------------------------------
$decimal_result = hexdec($hexadecimal_string);
// Print Pre-Formatted Results
// ---------------------------------------------
print($decimal_result);
// Output Here: "2.41978572002E+37"
// .....................................
// Format Results to View Whole All Digits in Integer
// ---------------------------------------------
// ( Note: All fractional value of the
// Hexadecimal variable are ignored
// in the conversion. )
$current_hashing_algorithm_decimal_result = number_format($decimal_result, 0, '', '');
// Print Formatted Results
// ---------------------------------------------
print($current_hashing_algorithm_decimal_result);
// Output Here: "24197857200151253041252346215207534592"
// .....................................
?>

Make dots between numbers in php

I would like to make dots between my total value.
If i have 425000 i would like it to show as 425.000
Is there a function in php that implodes dots for numbers or how can i do this then?
Use number_format for this:
$number = 425000;
echo number_format( $number, 0, '', '.' );
The above example means: format the number, using 0 decimal places, an empty string as the decimal point (as we're not using decimal places anyway), and use . as the thousands separator.
Unless of course I misunderstood your intent, and you want to format the number as a number with 3 decimal places:
$number = 425000;
echo number_format( $number, 3, '.', '' );
The above example means: format the number, using 3 decimal places, . as the decimal point (default), and an empty string as the thousands separator (defaults to ,).
If the default thousands separator is good enough for you, you can just use 1:
$number = 425000;
echo number_format( $number, 3 );
1) Mind you: number_format accepts either 1, 2 or 4 parameters, not 3.
I guess you're looking for the number_format function.

PHP function to create 8 chars long hash ([a-z] = no numbers allowed)

I need PHP function that will create 8 chars long [a-z] hash from any input string.
So e.g. when I'll submit "Stack Overflow" it will return e.g. "gdqreaxc" (8 chars [a-z] no numbers allowed)
Perhaps something like:
$hash = substr(strtolower(preg_replace('/[0-9_\/]+/','',base64_encode(sha1($input)))),0,8);
This produces a SHA1 hash, base-64 encodes it (giving us the full alphabet), removes non-alpha chars, lowercases it, and truncates it.
For $input = 'yar!';:
mwinzewn
For $input = 'yar!!';:
yzzhzwjj
So the spread seems pretty good.
This function will generate a hash containing evenly distributed characters [a-z]:
function my_hash($string, $length = 8) {
// Convert to a string which may contain only characters [0-9a-p]
$hash = base_convert(md5($string), 16, 26);
// Get part of the string
$hash = substr($hash, -$length);
// In rare cases it will be too short, add zeroes
$hash = str_pad($hash, $length, '0', STR_PAD_LEFT);
// Convert character set from [0-9a-p] to [a-z]
$hash = strtr($hash, '0123456789', 'qrstuvwxyz');
return $hash;
}
By the way, if this is important for you, for 100,000 different strings you'll have ~2% chance of hash collision (for a 8 chars long hash), and for a million of strings this chance rises up to ~90%, if my math is correct.
function md5toabc($myMD5)
{
$newString = "";
for ($i = 0; $i < 16; $i+=2)
{
//add the first val of 0-15 to the second val of 0-15 for a range of 0-30
$myintval = hexdec(substr($myMD5, $i, $i +1) ) +
hexdec(substr($myMD5, $i+1, $i +2) );
// mod by 26 and add 97 to get to the lowercase ascii range
$newString .= chr(($myintval%26) + 97);
}
return $newString;
}
Note this introduces bias to various characters, but do with it what you will.
(Like when you roll two dice, the most common value is a 7 combined...) plus the modulo, etc...
one can give you a good a-p{8} (but not a-z) by using and modifying (the output of) a well known algo:
function mini_hash( $string )
{
$h = hash( 'crc32' , $string );
for($i=0;$i<8;$i++) {
$h{$i} = chr(96+hexdec($h{$i}));
}
return $h;
}
interesting set of constraints you posted there
how about
substr (preg_replace(md5($mystring), "/[1-9]/", ""), 0, 8 );
you could add a bit more entorpy by doing a
preg_replace($myString, "1", "g");
preg_replace($myString, "2", "h");
preg_replace($myString, "3", "i");
etc instead of stripping the digits.

Categories