엑셀 차트 색상 생성기 구현

1 개요[ | ]

generate_chart_colors
엑셀 차트 색상 생성기 구현

2 소스 코드[ | ]

<style> div { width: 120px; } </style>
<?php
function xmp($arr) { echo '<xmp>'; print_r($arr); echo '</xmp>'; }
function hexcolor2rgb($hexcolor) {
	$r = substr($hexcolor, 1, 2);
	$g = substr($hexcolor, 3, 2);
	$b = substr($hexcolor, 5, 2);
	return array( hexdec($r), hexdec($g), hexdec($b) );
}
function rgb2hexcolor($rgb) {
	return '#'.dechex($rgb[0]).dechex($rgb[1]).dechex($rgb[2]);
}
function generate_chart_colors($num) {
	$low_colors = array('#29517b', '#7b3029', '#637531', '#4A3863', '#29697B', '#9C5D29');
	$high_colors = array('#C6CFE7', '#E7C7C6', '#D6DFC6', '#D6CFDE', '#CEDFEF', '#FFDBCE');

	$low_rgbs = array_map( 'hexcolor2rgb', $low_colors );
	$high_rgbs = array_map( 'hexcolor2rgb', $high_colors );

	$y_count = ceil( $num / 6 ) - 1;
	$colors = array();
	for( $i=0; $i<$num; $i++ ) {
		$x = $i % 6;
		$y = $i / 6;
		$low_rgb = $low_rgbs[$x];
		$high_rgb = $high_rgbs[$x];
		$r = $low_rgb[0] + ( $high_rgb[0] - $low_rgb[0] ) * $y / $y_count;
		$g = $low_rgb[1] + ( $high_rgb[1] - $low_rgb[1] ) * $y / $y_count;
		$b = $low_rgb[2] + ( $high_rgb[2] - $low_rgb[2] ) * $y / $y_count;
		$colors[] = rgb2hexcolor( array($r, $g, $b));
	}
	return $colors;
}

// Generate
$num = 20;
$colors = generate_chart_colors( $num );

// View
for( $i=0; $i<$num; $i++ ) {
	$color = $colors[$i];
	echo "<div style='float: left; background-color: $color'>[$i] $color</div>";
	if( $i % 6 == 5 ) echo "<div style='clear: both;'></div>";
}

echo "<div style='clear: both;'></div>";
xmp( $colors );

3 같이 보기[ | ]

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