RGBカラーの値をHSVカラー値に変換するコードを紹介します。
はじめに
Webアプリケーションプロジェクトを新規に作成します。
UI
下図のUIを準備します。
コード
下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace ColorCalc
{
public partial class ColorToHsv : System.Web.UI.Page
{
protected void Button_Convert_Click(object sender, EventArgs e)
{
int R = 0;
int G = 0;
int B = 0;
if (RadioButton_RGBInput.Checked == true) {
try {
R = Convert.ToInt32(TextBox_R.Text);
G = Convert.ToInt32(TextBox_G.Text);
B = Convert.ToInt32(TextBox_B.Text);
}
catch (FormatException exc) {
}
}
else {
try {
Color inputColorC = ColorTranslator.FromHtml(TextBox_WebColor.Text);
R = inputColorC.R;
G = inputColorC.G;
B = inputColorC.B;
}
catch (Exception exc) {
}
}
double dr = (double)R / (double)255;
double dg = (double)G / (double)255;
double db = (double)B / (double)255;
double maxv = Math.Max(Math.Max(dr, dg), db);
double minv = Math.Min(Math.Min(dr, dg), db);
double dh=0;
if (maxv - minv == 0) {
TextBox_H.Text = "未定義 (0)";
}
else {
if (maxv == dr) {
dh = 60 * ((dg - db) / (maxv - minv)) + 0;
}
else if (maxv == dg) {
dh = 60 * ((db - dr) / (maxv - minv)) + 120;
}
//else if (maxv == db) {
else {
dh = 60 * ((dr - dg) / (maxv - minv)) + 240;
}
if (dh < 0) dh += 360;
TextBox_H.Text = Convert.ToString(dh);
}
double ds = (maxv - minv) / maxv;
double dv = maxv;
if (maxv <= 0) {
TextBox_S.Text = "未定義 (0)";
TextBox_SPercent.Text = "未定義 (0)";
}
else {
TextBox_S.Text = Convert.ToString(ds);
int sper = (int)Math.Truncate(ds * 100);
TextBox_SPercent.Text = Convert.ToString(sper);
}
TextBox_V.Text = Convert.ToString(dv);
int vper = (int)Math.Truncate(dv * 100);
TextBox_VPercent.Text = Convert.ToString(vper);
Color inputColor = Color.FromArgb(R, G, B);
string strColor = string.Format("#{0:X2}{1:X2}{2:X2}",
inputColor.R, inputColor.G, inputColor.B);
Label_AddditionalInfo.Text = string.Format(
"WebColor:{0:s}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGB値({1:d},{2:d},{3:d})",
strColor, inputColor.R, inputColor.G, inputColor.B);
Literal_ColorBox.Text = string.Format(
"<div style=\"background-color:{0:s};width:64px;height:64px;float:left;\"></div>",
strColor);
}
}
}
解説
数式の詳細は
こちらのページを参照してください。
数式のみを表したものは下図になります。
コード解説
int R = 0;
int G = 0;
int B = 0;
if (RadioButton_RGBInput.Checked == true) {
try {
R = Convert.ToInt32(TextBox_R.Text);
G = Convert.ToInt32(TextBox_G.Text);
B = Convert.ToInt32(TextBox_B.Text);
}
catch (FormatException exc) {
}
}
else {
try {
Color inputColorC = ColorTranslator.FromHtml(TextBox_WebColor.Text);
R = inputColorC.R;
G = inputColorC.G;
B = inputColorC.B;
}
catch (Exception exc) {
}
}
上記コードにより、入力用のテキストボックスから値を取得します。
if (maxv <= 0) {
TextBox_S.Text = "未定義 (0)";
TextBox_SPercent.Text = "未定義 (0)";
}
else {
TextBox_S.Text = Convert.ToString(ds);
int sper = (int)Math.Truncate(ds * 100);
TextBox_SPercent.Text = Convert.ToString(sper);
}
TextBox_V.Text = Convert.ToString(dv);
int vper = (int)Math.Truncate(dv * 100);
TextBox_VPercent.Text = Convert.ToString(vper);
Color inputColor = Color.FromArgb(R, G, B);
string strColor = string.Format("#{0:X2}{1:X2}{2:X2}",
inputColor.R, inputColor.G, inputColor.B);
Label_AddditionalInfo.Text = string.Format(
"WebColor:{0:s}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGB値({1:d},{2:d},{3:d})",
strColor, inputColor.R, inputColor.G, inputColor.B);
Literal_ColorBox.Text = string.Format(
"<div style=\"background-color:{0:s};width:64px;height:64px;float:left;\"></div>",
strColor);
計算結果をテキストボックスに表示する部分です。テキストボックスに計算結果を表示するだけでなく、Literlコントロールに入力値の色を背景色とする四角の枠を表示します。
実行結果
プロジェクトを実行します。下図の画面が表示されます。
RGB値を入力し[変換]ボタンを押すと、HSV値が下部のテキストボックスに表示されます。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2018-02-26
作成日: 2012-12-12