<?xml version="1.0" encoding="UTF-8"?><!-- generator="bbPress" -->

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>

<channel>
<title>フォーラム トピック: 背景色から文字色を算出したい</title>
<link>http://ja.forums.wordpress.org/</link>
<description>フォーラム トピック: 背景色から文字色を算出したい</description>
<language>en</language>
<pubDate>Wed, 17 Mar 2010 21:32:38 +0000</pubDate>

<item>
<title>shokun0803 :  "背景色から文字色を算出したい"</title>
<link>http://ja.forums.wordpress.org/topic/2484#post-10129</link>
<pubDate>Fri, 18 Sep 2009 01:35:52 +0000</pubDate>
<dc:creator>shokun0803</dc:creator>
<guid isPermaLink="false">10129@http://ja.forums.wordpress.org/</guid>
<description>&#60;p&#62;jim912さん、身勝手な投稿に返答ありがとうございます。&#60;/p&#62;
&#60;p&#62;実は「白黒決め打ち」はどうなの？と思っていたのですが、色々試しているうちに#666以上、#999以下が背景の場合はw3cのいう計算式で満たせる文字色がない、ということになるようで、でもこれは色盲の方などが見る場合などのことなど全ての条件を含めての場合であって、通常の人が見る限り#666なら#fffでも十分見えると感じました。&#60;/p&#62;
&#60;p&#62;そこで、jim912さんのおっしゃるとおり（中間色さえ無視で）白黒決め打ちでソースを組んでみたところ、実に十分実用に耐えうる結果がでました。しかもソースはシンプルです;)&#60;/p&#62;
&#60;p&#62;参考ソース：&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;/* --------------------------------------------
 * 背景色の指定
 * -------------------------------------------- */

function bc_background_color() {
	global $post;
	$str = get_post_meta($post-&#38;gt;ID, &#38;#39;背景色&#38;#39;, true);
	$str = strtoupper($str);
	if( substr($str, 0, 1) == &#38;quot;#&#38;quot; ) {
		if( strlen($str) == 7 ) {
			$r = hexdec(substr($str, 1, 2));
			$g = hexdec(substr($str, 3, 2));
			$b = hexdec(substr($str, 5, 2));
		} elseif( strlen($str) == 4 ) {
			$r = substr($str, 1, 1);
			$g = substr($str, 2, 1);
			$b = substr($str, 3, 1);
			$r = hexdec($r.$r);
			$g = hexdec($g.$g);
			$b = hexdec($b.$b);
		}
	}
	$bright = (($r*299)+($g*587)+($b*114))/1000;
	if( $bright &#38;gt; 127.5 ) {
		$t_color = &#38;quot;000000&#38;quot;;
	} else {
		$t_color = &#38;quot;FFFFFF&#38;quot;;
	}
	if( $str ) {
		echo &#38;quot; style=\&#38;quot;background-color:{$str};color:#{$t_color}\&#38;quot;&#38;quot;;
	}
	return;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;まだまだ改良の余地がありますが（色名とか入力されるとアウト）、なんとか解決に繋がりました。本当にありがとうございました。
&#60;/p&#62;</description>
</item>
<item>
<title>jim912 :  "背景色から文字色を算出したい"</title>
<link>http://ja.forums.wordpress.org/topic/2484#post-10113</link>
<pubDate>Thu, 17 Sep 2009 16:08:50 +0000</pubDate>
<dc:creator>jim912</dc:creator>
<guid isPermaLink="false">10113@http://ja.forums.wordpress.org/</guid>
<description>&#60;p&#62;shokun0803さん、こんにちは。&#60;/p&#62;
&#60;p&#62;あまり工夫がないかもしれませんが、まずは落としどころとして下記のようなロジックではどうでしょう。&#60;/p&#62;
&#60;p&#62;背景色の明度が125以下なら文字色は白、130以上なら黒、その間なら白と黒で背景色との色差の大きい方を文字色とする。&#60;/p&#62;
&#60;p&#62;これで、まずは明度の差が確保され、見にくいテキストになることはなくなるはずです。
&#60;/p&#62;</description>
</item>
<item>
<title>shokun0803 :  "背景色から文字色を算出したい"</title>
<link>http://ja.forums.wordpress.org/topic/2484#post-10105</link>
<pubDate>Thu, 17 Sep 2009 08:21:28 +0000</pubDate>
<dc:creator>shokun0803</dc:creator>
<guid isPermaLink="false">10105@http://ja.forums.wordpress.org/</guid>
<description>&#60;p&#62;すみません、すみません、wordpress上で実現しようとしていますが、正確にはwordpressの問題ではありません。phpでのロジックの問題なのです・・・&#60;br /&#62;
迷惑になってしまうかもしれないのでその他の話題で投稿させていただきます。お時間のある技術者の方がいらしたらお手数ですがご教授願います。&#60;/p&#62;
&#60;p&#62;タイトルどおり、背景色のRGBから見やすいテキストの文字色を計算で算出できないかと試行錯誤しております。&#60;br /&#62;
計算式の元になるものはW3CのノートおよびWCAG2.0に示されている基準値とされている計算式で、&#60;a href=&#34;http://www.kanzaki.com/docs/html/color-check&#34;&#62;こちらの計算式&#60;/a&#62;を参考にしています。&#60;/p&#62;
&#60;p&#62;カスタムフィールドにRGBで背景色を指定し、&#60;code&#62;&#38;lt;div class=&#38;quot;post&#38;quot;&#38;gt;&#60;/code&#62;タグにstyleで書き出しをします。ここまではいいのですが、このとき文字色もstyleで書き出そうと思います。&#60;br /&#62;
※なぜカスタムフィールドで文字色も指定させないかなどのあたりは事情をお察しください。&#60;/p&#62;
&#60;p&#62;カスタムフィールドのRGBを取得して色の差500を目指すソースを記述してみましたが、中間色の背景を指定すると文字色も中間色になってしまいあえなく失敗でした。&#60;br /&#62;
根本から見直さなくてはならないと思いますが、手詰まりです。お力添えいただけましたら幸いです。&#60;/p&#62;
&#60;p&#62;参考ソース：（破綻しているソースです・・・）&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;/* --------------------------------------------
 * 背景色の指定
 * -------------------------------------------- */

function bc_background_color() {
	global $post;
	$str = get_post_meta($post-&#38;gt;ID, &#38;#39;背景色&#38;#39;, true);
	if( substr($str, 0, 1) == &#38;quot;#&#38;quot; ) {
		if( strlen($str) == 7 ) {
			$r = hexdec(substr($str, 1, 2));
			$g = hexdec(substr($str, 3, 2));
			$b = hexdec(substr($str, 5, 2));
		} elseif( strlen($str) == 4 ) {
			$r = substr($str, 1, 1);
			$g = substr($str, 2, 1);
			$b = substr($str, 3, 1);
		} else {

		}
	}
	function rgb_chk($str) {
		if( $str &#38;gt; 128 ) {
			$num = 0;
		} else {
			$num = 255;
		}
		$str = abs($str - 128);
		return array($str,$num);
	}
	$bright = (($r*299)+($g*587)+($b*114))/1000;
	$sr = rgb_chk($r);
	$sg = rgb_chk($g);
	$sb = rgb_chk($b);
	echo &#38;quot;R = {$r} G = {$g} B = {$b}&#38;lt;br /&#38;gt;\n&#38;quot;;
	echo &#38;quot;sR = {$sr[0]} sG = {$sg[0]} sB = {$sb[0]}&#38;lt;br /&#38;gt;\n&#38;quot;;
	$srgb = 500 - ( $sr[0] + $sg[0] + $sb[0] );
	echo &#38;quot;srgb = {$srgb}&#38;lt;br /&#38;gt;\n&#38;quot;;
	$rt = $sr[1] + (($srgb / ( $r + $g + $b )) * $r);
	$gt = $sg[1] + (($srgb / ( $r + $g + $b )) * $g);
	$bt = $sb[1] + (($srgb / ( $r + $g + $b )) * $b);
	if( $rt &#38;gt; 255 ) {
		$num = ceil(($rt - 255) / 2);
		$rt = &#38;quot;FF&#38;quot;;
		$gt = dechex($gt + $num);
		$bt = dechex($bt + $num);
	} elseif( $gt &#38;gt; 255 ) {
		$num = ceil(($gt - 255) / 2);
		$gt = &#38;quot;FF&#38;quot;;
		$rt = dechex($rt + $num);
		$bt = dechex($bt + $num);
	} elseif( $bt &#38;gt; 255 ) {
		$num = ceil(($bt - 255) / 2);
		$bt = &#38;quot;FF&#38;quot;;
		$rt = dechex($rt + $num);
		$gt = dechex($gt + $num);
	} else {
		$rt = dechex($rt + $num);
		$gt = dechex($gt + $num);
		$bt = dechex($bt + $num);
	}
	echo &#38;quot;Rt = {$rt} Gt = {$gt} Bt = {$bt}&#38;lt;br /&#38;gt;\n&#38;quot;;
	if( $str ) {
		echo &#38;quot; style=\&#38;quot;background-color:{$str};color:#{$rt}{$gt}{$bt}\&#38;quot;&#38;quot;;
	}
	return;
}&#60;/code&#62;&#60;/pre&#62;</description>
</item>

</channel>
</rss>
