"함수 str2rgb()"의 두 판 사이의 차이

13번째 줄: 13번째 줄:
<source lang='php'>
<source lang='php'>
function str2rgb( $str, $salt='' ) {
function str2rgb( $str, $salt='' ) {
   return substr(md5($str.$salt),0,6);
   return '#'.substr(md5($str.$salt),0,6);
}
}
</source>
</source>
22번째 줄: 22번째 줄:
   $g = hexdec(substr($code,2,2))/2+127;
   $g = hexdec(substr($code,2,2))/2+127;
   $b = hexdec(substr($code,4,2))/2+127;
   $b = hexdec(substr($code,4,2))/2+127;
   return dechex($r).dechex($g).dechex($b);
   return '#'.dechex($r).dechex($g).dechex($b);
}
}
</source>
</source>

2020년 5월 3일 (일) 01:40 판

string to color
str2rgb()
string2rgb()
string2color()
StringToRGB()

1 PHP

function str2rgb( $str, $salt='' ) {
  return '#'.substr(md5($str.$salt),0,6);
}
function str2rgb( $str, $salt='' ) {
  $code = dechex(crc32(md5($str.$salt)));
  $r = hexdec(substr($code,0,2))/2+127;
  $g = hexdec(substr($code,2,2))/2+127;
  $b = hexdec(substr($code,4,2))/2+127;
  return '#'.dechex($r).dechex($g).dechex($b);
}

2 Python

import hashlib
def str2rgb(s):
    return '#'+hashlib.md5(s.encode('utf-8')).hexdigest()[:6]
print( str2rgb('hello') ) # #5d4140
print( str2rgb('world') ) # #7d7930

3 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}