Skip to content

Instantly share code, notes, and snippets.

@KEINOS
Last active June 27, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KEINOS/11706d56606f6f927c8a5e5c7fcf2674 to your computer and use it in GitHub Desktop.
Save KEINOS/11706d56606f6f927c8a5e5c7fcf2674 to your computer and use it in GitHub Desktop.
KEINOSのSourceforge,GoogleCodeなどで管理していた汎用クラスやユーザー関数です。
<?php
/* このスクリプトはSourceforgeに2007/10/16に作成されたものを
* GitHub(gist)に移管したものです。
* ===============================================
* cColor クラス
* ===============================================
* ■用途
* 主にCSSなどで色味を統一させるために、色相を保ったまま、
* 下記を行うことができます。
*
* 1) 明度を変える
* 2) 色を混ぜる
* 3) 強調する
* 4) WEBセーフ・カラーに変換する
*
* ■使用例
*
* $sColor_original = "#b4af91";
*
* include_once("class.cColor.php");
* $oColor = new cColor( $sColor_original );
*
* $sColor_bright = $oColor->brightness( 50 );
* $sColor_dark = $oColor->brightness( -50 );
* $sColor_mix = $oColor->mix( "#c030ff", 50 );
* $sColor_strong = $oColor->strong( 64 );
* $sColor_websafe = $oColor->websafe();
*
*
* 【メッソッド】
*
* ■基本色(初期設定値)を取り出す(value)
*
* $oColor -> value;
*
* ■明るさを変える(brightness)
*
* $oColor -> brightness( 80 );
*
* 用途   : 引数がプラス値の場合は白に、マイナス値の場合は黒に近づけた値を返します。
* 第一引数 : int : -100〜100
*
* 引数の値が、100で黒(#000000)、-100で(#FFFFFF)になります。
* 0はオリジナル色(オブジェクト作成時に指定した色)のままです。
* 下記サンプルは、オリジナル色から黒になるまでの80%黒に近づけた値を返します。
*
* $oColor -> brightness( -80 );
*
*
* ■色を混ぜる(mix)
*
* $oColor -> mix( "#c03000", 50 );
*
* 用途   : オリジナル色(オブジェクト作成時に指定した色)を、引数の色に近づけます。
* 第一引数 : string : 混ぜる色
* 第二引数 : int : 混ぜる割合(0〜100)
*
* 値はプラス値のみです。0でオリジナル色、100で引数の色になります。
* 上記サンプルは、オリジナルと引数の色の中間色。( 50% )
*
* ■色の強調(strong)
*
* $oColor -> strong( 50 );
*
* 用途   : 色相を保ちながらオリジナル色(オブジェクト作成時に指定した色)を元に白
*      もしくは黒に近づけます。
* 第一引数 : int : 0〜100
*
* 値はプラス値のみです。元の色を元に、白に近づけるか黒に近づけるか判断します。
* 上記サンプルは、黒もしくは白へ50%強調した値を返します。
*
* ■Webセーフカラーに変換
*
* $oColor -> websafe();
*
* 用途   : Webセーフカラーに変換した値を返します。
* 第一引数 : string
*
* 与えられた色をWebセーフカラーに変換して返します。引数を指定しない場合は、オリジナル
* 色を返します。
* 下記サンプルは"#b73909"をWebセーフカラーに変換した値("#cc3300")を返します。
*
* $oColor -> websafe("#b73909");
*
*
*/
class cColor{
var $r;
var $g;
var $b;
var $rgb;
var $value;
//コンストラクタ
function cColor( $sColor = "" ){
$sColor = Empty( $sColor ) ? $this->Random() : $sColor;
$aTemp = $this->StrToRGB( $sColor );
$this -> r = $aTemp[0];
$this -> g = $aTemp[1];
$this -> b = $aTemp[2];
$this -> rgb = $aTemp;
$this -> value = $sColor;
}
//$this->value値の書き換え
function set( $s = "" ){
if( Empty( $s ) ) return FALSE;
return ( $this->cColor( $s ) );
}
//#xxxxxxを10進RGBの配列に変換
function StrToRGB( $sColor ){
$sColor = str_replace( '#', '', $sColor );
$sColor = strtolower( $sColor );
$len = strlen( $sColor );
if( ! ereg( '[^[:xdigit:]]+', $sColor ) && ( $len == "6" || $len == "3" ) ){
$d = $len /3;
$r = substr( $sColor, 0*$d, $d );
$g = substr( $sColor, 1*$d, $d );
$b = substr( $sColor, 2*$d, $d );
$a = ( $len == "3" ) ? array( $r . $r, $g . $g, $b . $b ) : array( $r, $g, $b ); //#FFF 形式の場合
return $this->HexDecRGB( $a );
}
else{
return FALSE;
}
}
//ランダムな色を生成
function Random(){
$r = mt_rand( 0, 255 );
$g = mt_rand( 0, 255 );
$b = mt_rand( 0, 255 );
return $this->FormatHex( array( $r, $g, $b ) );
}
//10進RGBの配列を16進RGBに変換
function HexDecRGB( $a ){
return array( hexdec( $a[0] ), hexdec( $a[1] ), hexdec( $a[2] ) );
}
//10進RGBを#xxxxxx形式で返す
function FormatHex( $a = null ){
if( $a == null ){
$a = $this->rgb;
}
$r = dechex( $a[0] );
$g = dechex( $a[1] );
$b = dechex( $a[2] );
//一桁を二桁に
$r = (strlen($r) == 1) ? '0' . $r : $r;
$g = (strlen($g) == 1) ? '0' . $g : $g;
$b = (strlen($b) == 1) ? '0' . $b : $b;
return "#" . $r . $g . $b;
}
//明度(輝度)値の算出
function GetBrightnessDepth( $s ){
$a = $this->StrToRGB( $s );
return round( MAX( $a[0], $a[1], $a[2] ) + MIN( $a[0], $a[1], $a[2] ), -1 ) / 2;
}
//明度の変更
function brightness( $i, $s = null ){
if( $s <> null ){
$aRGB = $this->StrToRGB( $s );
}
else{
$aRGB = $this->rgb;
}
$iLevel = 100;
if($i>0){
$i = ( $i > 100 ) ? 100 : $i;
$r_from = $aRGB[0]; $r_to = 255;
$g_from = $aRGB[1]; $g_to = 255;
$b_from = $aRGB[2]; $b_to = 255;
}
else{
$i = ( $i < -100 ) ? -100 : $i;
$i = $i * -1;
$r_from = $aRGB[0]; $r_to = 0;
$g_from = $aRGB[1]; $g_to = 0;
$b_from = $aRGB[2]; $b_to = 0;
}
$r = round((($r_from * ($iLevel - $i)) + ($r_to * $i)) / $iLevel);
$g = round((($g_from * ($iLevel - $i)) + ($g_to * $i)) / $iLevel);
$b = round((($b_from * ($iLevel - $i)) + ($b_to * $i)) / $iLevel);
$r = ($r > 255) ? 255 : ($r < 0) ? 0 : $r;
$g = ($g > 255) ? 255 : ($g < 0) ? 0 : $g;
$b = ($b > 255) ? 255 : ($b < 0) ? 0 : $b;
return $this -> FormatHex( array( $r, $g, $b) );
}
//Webセーフカラーへの変換
function websafe( $s = null ){
if( $s == null ){
$aRGB = $this->rgb;
}
else{
$aRGB = $this->StrToRGB( $s ); //10進のRGB配列へ
}
$r = $this -> GetNearestWebSafe( $aRGB[0] );
$g = $this -> GetNearestWebSafe( $aRGB[1] );
$b = $this -> GetNearestWebSafe( $aRGB[2] );
return $this -> FormatHex( array( $r, $g, $b ));
//return "({$r})({$g})({$b})";
}
//近似値を返す
function GetNearestWebSafe( $i ){
// 0/51/102/153/204/255
$iHalf = $this->Compare( $i, 102, 153 );
if( $iHalf == 102 ){
$iHalf = $this->Compare( $i, 51, 102 );
if( $iHalf == 51 ){
$iHalf = $this->Compare( $i, 0, 51 );
return ( $iHalf == 0 ) ? 0 : 51;
}
else{
return 102;
}
}
else{
$aThreshold = array( 153, 204, 255 );
$iHalf = $this->Compare( $i, 204, 255 );
if( $iHalf == 204 ){
$iHalf = $this->Compare( $i, 153, 204 );
return ( $iHalf == 204 ) ? 204 : 153;
}
else{
return 255;
}
}
}
function Compare( $target, $a, $b ){
$a2 = abs( $target - $a );
$b2 = abs( $target - $b );
return ( $a2 < $b2 ) ? $a : $b;
//return "{$target}:{$a}:{$b}:{$a2}:{$b2}";
}
function mix( $sTo, $i ){
$iLevel = 100;
$aRGB_From = $this->rgb;
$aRGB_To = $this->StrToRGB( $sTo );
$i = ( $i > 100 ) ? 100 : ($i < 0 ) ? 0 : $i;
$r_from = $aRGB_From[0]; $r_to = $aRGB_To[0];
$g_from = $aRGB_From[1]; $g_to = $aRGB_To[1];
$b_from = $aRGB_From[2]; $b_to = $aRGB_To[2];
$r = round((($r_from * ($iLevel - $i)) + ($r_to * $i)) / $iLevel);
$g = round((($g_from * ($iLevel - $i)) + ($g_to * $i)) / $iLevel);
$b = round((($b_from * ($iLevel - $i)) + ($b_to * $i)) / $iLevel);
$r = ( $r > 255 ) ? 255 : ( $r < 0 ) ? 0 : $r;
$g = ( $g > 255 ) ? 255 : ( $g < 0 ) ? 0 : $g;
$b = ( $b > 255 ) ? 255 : ( $b < 0 ) ? 0 : $b;
return $this->FormatHex( array( $r, $g, $b ) );
}
function strong( $i, $s = null ){
if( $s <> null ){
$aRGB = $this->StrToRGB( $s );
}
else{
$aRGB = $this->rgb;
}
$iLevel = 100;
$i = ( $i > 100) ? 100 : ( $i < 0 ) ? 0 : $i;
$r_from = $aRGB[0];
$g_from = $aRGB[1];
$b_from = $aRGB[2];
$iSum = array_sum( $aRGB );
$r_to = ( $iSum < 384 ) ? 255 : 0;
$g_to = ( $iSum < 384 ) ? 255 : 0;
$b_to = ( $iSum < 384 ) ? 255 : 0;
$r = round((($r_from * ($iLevel - $i)) + ($r_to * $i)) / $iLevel);
$g = round((($g_from * ($iLevel - $i)) + ($g_to * $i)) / $iLevel);
$b = round((($b_from * ($iLevel - $i)) + ($b_to * $i)) / $iLevel);
$r = ( $r > 255 ) ? 255 : ( $r < 0 ) ? 0 : $r;
$g = ( $g > 255 ) ? 255 : ( $g < 0 ) ? 0 : $g;
$b = ( $b > 255 ) ? 255 : ( $b < 0 ) ? 0 : $b;
return $this->FormatHex( array( $r, $g, $b ) );
}
}
?>
<?php
/* =======================================================================
* Googleドキュメントの公開文書をテキスト形式で取得する。
* SmartyテンプレートやCSSなどのテキストをGoogleドキュメントで管理したい
* 場合に使います。
* 注意:元ドキュメントであらかじめ読み込みたい部分を<template>タグで囲う
* 必要があります。
*
* @param string URL of Published Google Document Text.
* @return テキスト
* ======================================================================= */
if ( ! function_exists( 'file_get_googletext' ) ) {
function file_get_googletext( $sURL ) {
$sTemplate = file_get_contents( $sURL );
if( preg_match_all( "/<body\s.*>([\s\S]*)<\/body>/i", $sTemplate, $matches )){
$sTemplate= $matches[0][0];
}
$sTemplate = trim( strip_tags( $sTemplate ) );
$sTemplate = mb_decode_numericentity( $sTemplate, array( 0, 0xffff, 0, 0xffff ), "UTF-8" );
$sTemplate = html_entity_decode( $sTemplate );
if( preg_match_all('/<template>([\s\S]*)<\/template>/i', $sTemplate, $matches )){
$sTemplate = $matches[1][0];
}
return $sTemplate;
}
}
?>
<?php
/**
* 環境をUTF-8化する
*
*/
if(!function_exists('utf_8')){
function utf_8(){
setlocale(LC_ALL, 'ja_JP');
mb_language('ja');
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
ob_start("mb_output_handler");
}
}
//エイリアス関数
if(!function_exists('utf8')){
function utf8(){
return utf_8();
}
}
if(!function_exists('mb_language')){
die("KEINOS' PHP application requires mb_language.");
}else{
utf_8();
}
?>

目次

Class一覧

  • class.cColor.php : CSSなどで、色味を統一させるために、色相を保ったまま、明度の変更、色の混合、色の強調、ウェエブセーフカラーなどに変換するクラスです。

Function一覧

  • function.file_get_googletext.php : 一般公開されたGoogle文書をSmartyなどのテンプレート・エンジンのテンプレートとして使うために、テキストの文字列として返す関数。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment