RGBToColor関数

本ページには広告が含まれています。

引数に指定された赤成分、緑成分、青成分、もしくは#RRGGBB形式の文字列からColor値を求めます。

構文
  1. Double = RGBToColor( r, g, b )
引数
r 必須
赤成分もしくは#RRGGBB形式の文字列
g 省略可
緑成分。r#RRGGBB形式の文字列を指定した場合は省略。
b 省略可
青成分。r#RRGGBB形式の文字列を指定した場合は省略。
戻り値
Color値

プログラム

UWSC
//////////////////////////////////////////////////
// 【引数】
//   r : 赤成分もしくは#RRGGBB形式の文字列 
//   g : 緑成分。rで#RRGGBB形式の文字列を指定した場合は省略。 
//   b : 青成分。rで#RRGGBB形式の文字列を指定した場合は省略。 
// 【戻り値】
//   Color値 
//////////////////////////////////////////////////
FUNCTION RGBToColor(r, g = EMPTY, b = EMPTY)
	IFB COPY(r, 1, 1) = "#" THEN
		DIM color = 0
		FOR i = 1 TO 3
			color = color + VAL(hexToDec(COPY(r, i * 2, 2), FALSE)) * POWER(256, i - 1)
		NEXT
		RESULT =  color
	ELSE
		RESULT = r + g * 256 + b * 65536
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   bin : 2進数 
//   signFlg : 符号付きならばTrue 
// 【戻り値】
//   10進数に変換した値 
//////////////////////////////////////////////////
FUNCTION binToDec(bin, signFlg = TRUE)
	DIM dec = 0
	DIM decimalFlg = IIF(POS(".", bin), TRUE, FALSE)
	IFB COPY(bin, 1, 1) = "1" AND signFlg THEN
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg, POS(".", bin) - LENGTH(bin), 0)
		DIM dec2 = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			dec2 = dec2 + POWER(2, i)
		NEXT
		DIM a = binToDec(bin, FALSE)
		DIM b = dec2
		dec = -1 * (bitXor(a, b) + POWER(2, lsb))
	ELSE
		IFB decimalFlg THEN
			DIM integer = COPY(bin, 1, POS(".", bin) - 1)
			DIM decimal = COPY(bin, POS(".", bin) + 1)
			FOR i = 1 TO LENGTH(decimal)
				dec = dec + COPY(decimal, i, 1) * POWER(2, -1 * i)
			NEXT
		ELSE
			integer = bin
		ENDIF
		FOR i = 1 TO LENGTH(integer)
			dec = dec + COPY(integer, i, 1) * POWER(2, LENGTH(integer) - i)
		NEXT
	ENDIF
	RESULT = dec
FEND

//////////////////////////////////////////////////
// 【引数】
//   arg1 : 数値1(10進数) 
//   arg2 : 数値2(10進数) 
// 【戻り値】
//   2つの数値のビット毎の排他的論理和 
//////////////////////////////////////////////////
FUNCTION bitXor(arg1, arg2)
	DIM args[1] = arg1, arg2
	DIM bins[1]
	DIM decimals[1]
	DIM integers[1]
	DIM keta[1]
	IFB ABS(arg1) <> arg1 OR ABS(arg2) <> arg2 THEN
		RESULT = ERR_VALUE
		EXIT
	ENDIF
	FOR i = 0 TO 1
		bins[i] = decToBin(args[i])
		decimals[i] = 0
		IFB POS(".", bins[i]) <> 0 THEN
			integers[i] = COPY(bins[i], 1, POS(".", bins[i]) - 1)
			decimals[i] = COPY(bins[i], POS(".", bins[i]) + 1)
		ELSE
			integers[i] = bins[i]
		ENDIF
	NEXT
	keta[0] = IIF(LENGTH(integers[0]) > LENGTH(integers[1]), LENGTH(integers[0]), LENGTH(integers[1]))
	integers[0] = strPad(integers[0], keta[0], "0", LEFT)
	integers[1] = strPad(integers[1], keta[0], "0", LEFT)
	keta[1] = IIF(LENGTH(decimals[0]) > LENGTH(decimals[1]), LENGTH(decimals[0]), LENGTH(decimals[1]))
	decimals[0] = strPad(decimals[0], keta[1], "0", RIGHT)
	decimals[1] = strPad(decimals[1], keta[1], "0", RIGHT)
	DIM bin = ""
	FOR i = 1 TO keta[0]
		bin = bin + (VAL(COPY(integers[0], i, 1)) XOR VAL(COPY(integers[1], i, 1)))
	NEXT
	bin = bin + "."
	FOR i = 1 TO keta[1]
		bin = bin + (VAL(COPY(decimals[0], i, 1)) XOR VAL(COPY(decimals[1], i, 1)))
	NEXT
	RESULT = binToDec(bin)
FEND

//////////////////////////////////////////////////
// 【引数】
//   dec : 10進数 
//   signFlg : 符号付きならばTrueを指定 
//   digits : 変換した2進数の桁数合わせを自動で行うかを示すブール値、もしくは桁数を表す数値(8,16,24,32,64のいずれか)を指定 
//   recursive : 再帰処理の深さ。処理する際に必要なだけで指定する必要はありません。 
// 【戻り値】
//   2進数に変換した値 
//////////////////////////////////////////////////
FUNCTION decToBin(dec, signFlg = TRUE, digits = FALSE, recursive = 1)
	IFB dec < 0 AND signFlg = FALSE THEN
		RESULT = ERR_VALUE
		EXIT
	ENDIF
	IFB VARTYPE(digits) <> VAR_BOOLEAN AND digits < CEIL(LOGN(2, ABS(dec))) + IIF(dec < 0, 1, 0) THEN
		RESULT = ERR_VALUE
		EXIT
	ENDIF
	DIM bin = ""
	DIM decimalFlg = IIF(POS(".", dec) <> 0, TRUE, FALSE)
	DIM negativeFlg = IIF(dec < 0, TRUE, FALSE)
	dec = ABS(dec)
	DIM integer = IIF(decimalFlg, COPY(dec, 1, POS(".", dec) - 1), dec)
	DIM offset = POWER(10, LENGTH(dec) - POS(".", dec))
	DIM decimal = IIF(decimalFlg, COPY(dec, POS(".", dec) + 1) / offset, 0)
	REPEAT
		bin = (integer MOD 2) + bin
		integer = INT(integer / 2)
	UNTIL integer = 0
	IFB decimalFlg THEN
		bin = bin + "."
		DIM loop = 0
		REPEAT
			loop = loop + 1
			decimal = decimal * 2
			bin = bin + IIF(decimal >= 1, "1", "0")
			IF decimal > 1 THEN decimal = decimal - 1
		UNTIL decimal = 1 OR loop >= 64
		WHILE loop MOD 4 <> 0
			loop = loop + 1
			bin = bin + "0"
		WEND
	ENDIF
	IFB VARTYPE(digits) = VAR_BOOLEAN THEN
		bin = strPad(bin, CEIL(LENGTH(REPLACE(bin, ".", "")) / 8) * 8, "0", LEFT)
	ELSE
		bin = strPad(bin, digits, "0", LEFT)
	ENDIF
	IFB negativeFlg THEN
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg , POS(".", bin) - LENGTH(bin), 0)
		DIM a = binToDec(bin, FALSE)
		DIM b = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			b = b + POWER(2, i)
		NEXT
  		dec = bitXor(a, b) + POWER(2, lsb)
 		bin = decToBin(dec, signFlg, digits, recursive + 1)
	ENDIF
	IFB recursive = 1 THEN
		DIM bit = COPY(bin, 1, 1)
 		DIM len = LENGTH(REPLACE(bin, ".", ""))
		IF negativeFlg AND (bit = "0" OR len MOD 2 <> 0) THEN bin = strRepeat("1", IIF(len MOD 2 <> 0, 4, 8)) + bin
		IF !negativeFlg AND signFlg AND (bit = "1" OR len MOD 8 <> 0) THEN bin = strRepeat("0", IIF(len MOD 8 <> 0, 4, 8)) + bin
	ENDIF
	RESULT = bin
FEND

//////////////////////////////////////////////////
// 【引数】
//   dec : 10進数 
//   signFlg : 符号付きならばTrue 
//   recursive : 再帰処理の深さ。処理する際に必要なだけで指定する必要はありません。 
// 【戻り値】
//   16進数に変換した値 
//////////////////////////////////////////////////
FUNCTION decToHex(dec, signFlg = TRUE, recursive = 1)
	DIM hex = ""
	DIM array[] = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
	DIM decimalFlg = IIF(POS(".", dec) <> 0, TRUE, FALSE)
	DIM negativeFlg = IIF(dec < 0, TRUE, FALSE)
	dec = ABS(dec)
	DIM integer = IIF(decimalFlg, COPY(dec, 1, POS(".", dec) - 1), dec)
	DIM offset = POWER(10, LENGTH(dec) - POS(".", dec))
	DIM decimal = IIF(decimalFlg, COPY(dec, POS(".", dec) + 1) / offset, 0)
	REPEAT
		hex = array[integer MOD 16] + hex
		integer = INT(integer / 16)
	UNTIL integer = 0
	IFB decimalFlg THEN
		hex = hex + "."
		DIM loop = 0
		REPEAT
			loop = loop + 1
			decimal = decimal * 16
			hex = hex + array[decimal]
			offset = POWER(10, LENGTH(decimal) - POS(".", decimal))
			decimal = (decimal * offset - INT(decimal) * offset) / offset
		UNTIL decimal = 0 OR loop >= 4
	ENDIF
	DIM bin = hexToBin(hex)
	DIM keta = LENGTH(bin)
	IFB keta < 8 THEN
		bin = strPad(bin, decimalFlg + 8, "0", LEFT)
	ELSEIF keta < 16 THEN
		bin = strPad(bin, decimalFlg + 16, "0", LEFT)
	ELSEIF keta < 24 THEN
		bin = strPad(bin, decimalFlg + 24, "0", LEFT)
	ELSEIF keta < 32 THEN
		bin = strPad(bin, decimalFlg + 32, "0", LEFT)
	ELSE
		bin = strPad(bin, decimalFlg + 64, "0", LEFT)
	ENDIF
	IFB negativeFlg THEN
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg, POS(".", bin) - LENGTH(bin), 0)
		DIM a = hexToDec(hex, FALSE)
		DIM b = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			b = b + POWER(2, i)
		NEXT
		dec = bitXor(a, b) + POWER(2, lsb)
		hex = decToHex(dec, signFlg, recursive + 1)
	ENDIF
	IFB recursive = 1 THEN
		DIM bit = COPY(hexToBin(COPY(hex, 1, 1)), 1, 1)
		DIM len = LENGTH(REPLACE(hex, ".", ""))
		IF negativeFlg AND (bit = "0" OR len MOD 2 <> 0) THEN hex = strRepeat("F", IIF(len MOD 2 <> 0, 1, 2)) + hex
		IF !negativeFlg AND signFlg AND (bit = "1" OR len MOD 2 <> 0) THEN hex = strRepeat("0", IIF(len MOD 2 <> 0, 1, 2)) + hex
	ENDIF
	RESULT = hex
FEND

//////////////////////////////////////////////////
// 【引数】
//   hex : 16進数 
// 【戻り値】
//   2進数に変換した値 
//////////////////////////////////////////////////
FUNCTION hexToBin(hex)
	HASHTBL hb
	hb["0"] = "0000";	hb["1"] = "0001";	hb["2"] = "0010";	hb["3"] = "0011";
	hb["4"] = "0100";	hb["5"] = "0101";	hb["6"] = "0110";	hb["7"] = "0111";
	hb["8"] = "1000";	hb["9"] = "1001";	hb["A"] = "1010";	hb["B"] = "1011";
	hb["C"] = "1100";	hb["D"] = "1101";	hb["E"] = "1110";	hb["F"] = "1111";
	DIM bin = ""
	IFB POS(".", hex) <> 0 THEN
		FOR i = 1 TO LENGTH(hex)
			DIM str = COPY(hex, i, 1)
			IF str = "." THEN bin = bin + "."
			bin = bin + hb[str]
		NEXT
	ELSE
		FOR i = 1 TO LENGTH(hex)
			bin = bin + hb[COPY(hex, i, 1)]
		NEXT
	ENDIF
	RESULT = bin
FEND

//////////////////////////////////////////////////
// 【引数】
//   hex : 16進数 
//   signFlg : 符号付きならばTrue 
// 【戻り値】
//   10進数に変換した値 
//////////////////////////////////////////////////
FUNCTION hexToDec(hex, signFlg = TRUE)
	hex = STRCONV(hex, SC_UPPERCASE)
	DIM dec = 0
	DIM decimalFlg = IIF(POS(".", hex) <> 0, TRUE, FALSE)
	hex = IIF(LENGTH(REPLACE(hex,".", "" )) MOD 2 <> 0, "0", "") + hex
	DIM negativeFlg = IIF(COPY(hexToBin(hex), 1, 1) = "1", TRUE, FALSE)
	DIM sign = 1
	IF negativeFlg AND signFlg THEN sign = -1
	IFB negativeFlg AND signFlg THEN
		DIM bin = hexToBin(hex)
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg, POS(".", bin) - LENGTH(bin), 0)
		DIM a = hexToDec(hex, FALSE)
		DIM b = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			b = b + POWER(2, i)
		NEXT
		DIM dec2 = bitXor(a, b) + POWER(2, lsb)
		hex = decToHex(dec2)
	ENDIF
	integer = IIF(decimalFlg, COPY(hex, 1, POS(".", hex) - 1), hex)
	decimal = IIF(decimalFlg, COPY(hex, POS(".", hex) + 1), "0")
	FOR i = 1 TO LENGTH(integer)
		s = COPY(hex, i, 1)
		num = IIF(CHKNUM(s), s, ASC(s) - (ASC("A") - 10))
		dec = dec + num * POWER(16, LENGTH(integer) - i)
	NEXT
	FOR i = 1 TO LENGTH(decimal)
		s = COPY(decimal, i, 1)
		num = IIF(CHKNUM(s), s, ASC(s) - (ASC("A") - 10))
		dec = dec + num * POWER(16, -1 * i)
	NEXT
	RESULT = sign * dec
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   num : 符号を求める数値 
// 【戻り値】
//   1 : 正の数、0 : ゼロ、-1 : 負の数、ERR_VALUE : それ以外 
//////////////////////////////////////////////////
FUNCTION sign(num)
	SELECT TRUE
		CASE !CHKNUM(num)
			RESULT = ERR_VALUE
		CASE num > 0
			RESULT = 1
		CASE num = 0
			RESULT = 0
		CASE num < 0
			RESULT = -1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   input : 入力文字列 
//   length : 埋めたあとの長さ 
//   str : 埋める文字 
//   type : 埋める方向 
// 【戻り値】
//   指定文字で埋めた文字列 
//////////////////////////////////////////////////
FUNCTION strPad(input, length, str = " ", type = RIGHT)
	DIM s = ""
	SELECT type
		CASE LEFT
			FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
				s = s + str
			NEXT
			input = COPY(s, 1, length - LENGTH(input)) + input
		CASE RIGHT
			FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
				s = s + str
			NEXT
			input = input + COPY(s, 1, length - LENGTH(input))
	SELEND
	RESULT = input
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

使い方

各成分を数値で指定

赤成分・緑成分・青成分の数値を指定しColor値を取得します。

UWSC
PRINT RGBToColor(255, 128, 255)
結果
プレーンテキスト
16679167

#RRGGBB形式で指定

#RRGGBB形式の文字列を指定しColor値を取得します。

UWSC
PRINT RGBToColor("#FF80FF")
結果
プレーンテキスト
16679167

プログラム実行例

コロナのグラフ

UWSC
CONST xlDown = -4121
CONST xlColumnClustered = 51
CONST xlCategory = 1
CONST xlValue = 2
CONST xlLine = 4
CONST msoThemeColorText2 = 15
CONST msoThemeColorBackground1 = 14

DIM Excel = CREATEOLEOBJ("Excel.Application")

WITH Excel
	.Visible = TRUE
	.Workbooks.Open("D:\Downloads\covid19_data.csv")

	DIM ws = .ActiveSheet
	DIM ws2 = .Worksheets.Add(EMPTYPARAM, .Worksheets(.Worksheets.Count))
	ws2.Name = "7日間平均"
	ws.Range("1:1").Copy(ws2.Range("1:1"))
	ws.Range("A:D").Copy(ws2.Range("A:D"))
	ws.Range("W:AB").Copy(ws2.Range("W:AB"))
	ws2.Range("E8").Value = "=INT(SUM(covid19_data!E2:E8)/7)"
	ws2.Range("E8").AutoFill(ws2.Range("E8:V8"))
	DIM lastRow = ws.Range("B2").End(xlDown).Row	
	ws2.Range("E8:V8").AutoFill(ws2.Range("E8:V" + lastRow))

	DIM Chart

	// 検査数
	Chart = .Charts.Add
	.ActiveSheet.Move(EMPTYPARAM, .Sheets(.Sheets.Count))
	.ActiveSheet.Name = "検査数"

	WITH Chart
		.ChartType = xlColumnClustered
		.SetSourceData(ws.Range("covid19_data!$E:$E"))
	
		WITH .SeriesCollection(1)
			.Name = "日検査数"
			.XValues = "covid19_data!$Y2:$Y" + lastRow
			WITH .Format.Line
				.ForeColor.ObjectThemeColor = msoThemeColorText2
				.ForeColor.TintAndShade = 0
				.ForeColor.Brightness = 0.6000000238
				.Transparency = 0
			ENDWITH
		ENDWITH

		WITH .Axes(xlCategory)
			.MinimumScale = dateSerial(2020, 1, 1)		//43831
			.MajorUnit = 4
			.TickLabels.Font.Size = 14
		ENDWITH
	
		WITH .Axes(xlValue)
			.HasMajorGridLines = TRUE
			.HasMinorGridLines = TRUE
			.MinimumScale = 0
			.MajorUnit = 5000
			.MinorUnit = 1000
			.TickLabels.Font.Size = 14
		ENDWITH
	ENDWITH	

	// 陽性数
	Chart = .Charts.Add
	.ActiveSheet.Move(EMPTYPARAM, .Sheets(.Sheets.Count))
	.ActiveSheet.Name = "陽性者数"

	WITH Chart
		.ChartType = xlColumnClustered
		.SetSourceData(ws.Range("covid19_data!$G2:$G" + lastRow))
		.SeriesCollection.NewSeries
		.SeriesCollection(2).Values = "='7日間平均'!$G2:$G" + lastRow

		// 日付
		WITH .SeriesCollection(1)
			.Name = "日陽性数"
			.XValues = "covid19_data!$Y2:$Y" + lastRow
			WITH .Format.Line
				.ForeColor.ObjectThemeColor = msoThemeColorText2
				.ForeColor.TintAndShade = 0
				.ForeColor.Brightness = 0.6000000238
				.Transparency = 0
			ENDWITH
		ENDWITH
	
		// 7日間平均
		WITH .SeriesCollection(2)
			.Name = "7日間平均"
			.ChartType = xlLine
			.Format.Line.ForeColor.RGB = RGBToColor(0, 112, 192)
		ENDWITH
	
		// x軸
		WITH .Axes(xlCategory)
			.MinimumScale = dateSerial(2020, 1, 1)		//43831
			.MajorUnit = 4
			.TickLabels.Font.Size = 14
		ENDWITH
	
		// y軸
		WITH .Axes(xlValue)
			.HasMajorGridLines = TRUE
			.HasMinorGridLines = TRUE
			.MinimumScale = 0
			.MajorUnit = 5000
			.MinorUnit = 1000
			.TickLabels.Font.Size = 14
		ENDWITH
	ENDWITH

	// 死亡数
	Chart = .Charts.Add
	.ActiveSheet.Move(EMPTYPARAM, .Sheets(.Sheets.Count))
	.ActiveSheet.Name = "死亡数"
	
	WITH Chart
		.ChartType = xlColumnClustered
		.SetSourceData(ws.Range("covid19_data!$O2:$O" + lastRow))
		.SeriesCollection.NewSeries
		.SeriesCollection(2).Values = "='7日間平均'!$O2:$O" + lastRow

		WITH .SeriesCollection(1)
			.Name = "日死亡数"
			.XValues = "covid19_data!$Y2:$Y" + lastRow
			WITH .Format.Line
				.ForeColor.ObjectThemeColor = msoThemeColorBackground1
				.ForeColor.TintAndShade = 0
				.ForeColor.Brightness = -0.150000006
				.Transparency = 0
			ENDWITH
		ENDWITH

		WITH .SeriesCollection(2)
			.Name = "7日間平均"
			.ChartType = xlLine
			WITH .Format.Line
				.Weight = 2.5
				.Transparency = 0
				.ForeColor.ObjectThemeColor = msoThemeColorBackground1
				.ForeColor.TintAndShade = 0
				.ForeColor.Brightness = -0.5
			ENDWITH
		ENDWITH	

		WITH .Axes(xlCategory)
			.MinimumScale = dateSerial(2020, 1, 1)		//43831
			.MajorUnit = 4
			.TickLabels.Font.Size = 14
		ENDWITH

		WITH .Axes(xlValue)
			.HasMajorGridLines = TRUE		// 軸あり
			.HasMinorGridLines = TRUE		// 目盛線あり
			.MinimumScale = 0				// 最小値
			.TickLabels.Font.Size = 14		// フォントサイズ
		ENDWITH
	ENDWITH

	// 死亡累計
	Chart = .Charts.Add
	.ActiveSheet.Move(EMPTYPARAM, .Sheets(.Sheets.Count))
	.ActiveSheet.Name = "死亡累計"
	
	WITH Chart
		.ChartType = xlColumnClustered
		.SetSourceData(ws.Range("covid19_data!$P2:$P" + lastRow))
				
		WITH .SeriesCollection(1)
			.Name = "死亡累計"
			.XValues = "covid19_data!$Y2:$Y" + lastRow
			WITH .Format.Line
				.ForeColor.ObjectThemeColor = msoThemeColorBackground1
				.ForeColor.TintAndShade = 0
				.ForeColor.Brightness = -0.150000006
				.Transparency = 0
			ENDWITH
		ENDWITH
		
		WITH .Axes(xlCategory)
			.MinimumScale = dateSerial(2020, 1, 1)		//43831
			.MajorUnit = 4
			.TickLabels.Font.Size = 14
		ENDWITH

		WITH .Axes(xlValue)
			.HasMajorGridLines = TRUE		// 軸あり
			.MinimumScale = 0				// 最小値
			.TickLabels.Font.Size = 14		// フォントサイズ
		ENDWITH

		.ChartTitle.Delete

	ENDWITH

ENDWITH

//////////////////////////////////////////////////
// 【引数】
//   bin : 2進数 
//   signFlg : 符号付きならばTrue 
// 【戻り値】
//   10進数に変換した値 
//////////////////////////////////////////////////
FUNCTION binToDec(bin, signFlg = TRUE)
	DIM dec = 0
	DIM decimalFlg = IIF(POS(".", bin), TRUE, FALSE)
	IFB COPY(bin, 1, 1) = "1" AND signFlg THEN
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg, POS(".", bin) - LENGTH(bin), 0)
		DIM dec2 = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			dec2 = dec2 + POWER(2, i)
		NEXT
		DIM a = binToDec(bin, FALSE)
		DIM b = dec2
		dec = -1 * (bitXor(a, b) + POWER(2, lsb))
	ELSE
		IFB decimalFlg THEN
			DIM integer = COPY(bin, 1, POS(".", bin) - 1)
			DIM decimal = COPY(bin, POS(".", bin) + 1)
			FOR i = 1 TO LENGTH(decimal)
				dec = dec + COPY(decimal, i, 1) * POWER(2, -1 * i)
			NEXT
		ELSE
			integer = bin
		ENDIF
		FOR i = 1 TO LENGTH(integer)
			dec = dec + COPY(integer, i, 1) * POWER(2, LENGTH(integer) - i)
		NEXT
	ENDIF
	RESULT = dec
FEND

//////////////////////////////////////////////////
// 【引数】
//   arg1 : 数値1(10進数) 
//   arg2 : 数値2(10進数) 
// 【戻り値】
//   2つの数値のビット毎の排他的論理和 
//////////////////////////////////////////////////
FUNCTION bitXor(arg1, arg2)
	DIM args[1] = arg1, arg2
	DIM bins[1]
	DIM decimals[1]
	DIM integers[1]
	DIM keta[1]
	IFB ABS(arg1) <> arg1 OR ABS(arg2) <> arg2 THEN
		RESULT = ERR_VALUE
		EXIT
	ENDIF
	FOR i = 0 TO 1
		bins[i] = decToBin(args[i])
		decimals[i] = 0
		IFB POS(".", bins[i]) <> 0 THEN
			integers[i] = COPY(bins[i], 1, POS(".", bins[i]) - 1)
			decimals[i] = COPY(bins[i], POS(".", bins[i]) + 1)
		ELSE
			integers[i] = bins[i]
		ENDIF
	NEXT
	keta[0] = IIF(LENGTH(integers[0]) > LENGTH(integers[1]), LENGTH(integers[0]), LENGTH(integers[1]))
	integers[0] = strPad(integers[0], keta[0], "0", LEFT)
	integers[1] = strPad(integers[1], keta[0], "0", LEFT)
	keta[1] = IIF(LENGTH(decimals[0]) > LENGTH(decimals[1]), LENGTH(decimals[0]), LENGTH(decimals[1]))
	decimals[0] = strPad(decimals[0], keta[1], "0", RIGHT)
	decimals[1] = strPad(decimals[1], keta[1], "0", RIGHT)
	DIM bin = ""
	FOR i = 1 TO keta[0]
		bin = bin + (VAL(COPY(integers[0], i, 1)) XOR VAL(COPY(integers[1], i, 1)))
	NEXT
	bin = bin + "."
	FOR i = 1 TO keta[1]
		bin = bin + (VAL(COPY(decimals[0], i, 1)) XOR VAL(COPY(decimals[1], i, 1)))
	NEXT
	RESULT = binToDec(bin)
FEND

//////////////////////////////////////////////////
// 【引数】
//   year : 年 
//   month : 月 
//   day : 日 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION dateSerial(year, month, day)
	month = REPLACE(FORMAT(month, 2), " ", "0")
	day = REPLACE(FORMAT(day, 2), " ", "0")
	d = GETTIME(0, year + "/" + month + "/" + day)
	RESULT = uwscToSerial(d)
FEND

//////////////////////////////////////////////////
// 【引数】
//   dec : 10進数 
//   signFlg : 符号付きならばTrueを指定 
//   digits : 変換した2進数の桁数合わせを自動で行うかを示すブール値、もしくは桁数を表す数値(8,16,24,32,64のいずれか)を指定 
//   recursive : 再帰処理の深さ。処理する際に必要なだけで指定する必要はありません。 
// 【戻り値】
//   2進数に変換した値 
//////////////////////////////////////////////////
FUNCTION decToBin(dec, signFlg = TRUE, digits = FALSE, recursive = 1)
	IFB dec < 0 AND signFlg = FALSE THEN
		RESULT = ERR_VALUE
		EXIT
	ENDIF
	IFB VARTYPE(digits) <> VAR_BOOLEAN AND digits < CEIL(LOGN(2, ABS(dec))) + IIF(dec < 0, 1, 0) THEN
		RESULT = ERR_VALUE
		EXIT
	ENDIF
	DIM bin = ""
	DIM decimalFlg = IIF(POS(".", dec) <> 0, TRUE, FALSE)
	DIM negativeFlg = IIF(dec < 0, TRUE, FALSE)
	dec = ABS(dec)
	DIM integer = IIF(decimalFlg, COPY(dec, 1, POS(".", dec) - 1), dec)
	DIM offset = POWER(10, LENGTH(dec) - POS(".", dec))
	DIM decimal = IIF(decimalFlg, COPY(dec, POS(".", dec) + 1) / offset, 0)
	REPEAT
		bin = (integer MOD 2) + bin
		integer = INT(integer / 2)
	UNTIL integer = 0
	IFB decimalFlg THEN
		bin = bin + "."
		DIM loop = 0
		REPEAT
			loop = loop + 1
			decimal = decimal * 2
			bin = bin + IIF(decimal >= 1, "1", "0")
			IF decimal > 1 THEN decimal = decimal - 1
		UNTIL decimal = 1 OR loop >= 64
		WHILE loop MOD 4 <> 0
			loop = loop + 1
			bin = bin + "0"
		WEND
	ENDIF
	IFB VARTYPE(digits) = VAR_BOOLEAN THEN
		bin = strPad(bin, CEIL(LENGTH(REPLACE(bin, ".", "")) / 8) * 8, "0", LEFT)
	ELSE
		bin = strPad(bin, digits, "0", LEFT)
	ENDIF
	IFB negativeFlg THEN
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg , POS(".", bin) - LENGTH(bin), 0)
		DIM a = binToDec(bin, FALSE)
		DIM b = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			b = b + POWER(2, i)
		NEXT
  		dec = bitXor(a, b) + POWER(2, lsb)
 		bin = decToBin(dec, signFlg, digits, recursive + 1)
	ENDIF
	IFB recursive = 1 THEN
		DIM bit = COPY(bin, 1, 1)
 		DIM len = LENGTH(REPLACE(bin, ".", ""))
		IF negativeFlg AND (bit = "0" OR len MOD 2 <> 0) THEN bin = strRepeat("1", IIF(len MOD 2 <> 0, 4, 8)) + bin
		IF !negativeFlg AND signFlg AND (bit = "1" OR len MOD 8 <> 0) THEN bin = strRepeat("0", IIF(len MOD 8 <> 0, 4, 8)) + bin
	ENDIF
	RESULT = bin
FEND

//////////////////////////////////////////////////
// 【引数】
//   dec : 10進数 
//   signFlg : 符号付きならばTrue 
//   recursive : 再帰処理の深さ。処理する際に必要なだけで指定する必要はありません。 
// 【戻り値】
//   16進数に変換した値 
//////////////////////////////////////////////////
FUNCTION decToHex(dec, signFlg = TRUE, recursive = 1)
	DIM hex = ""
	DIM array[] = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
	DIM decimalFlg = IIF(POS(".", dec) <> 0, TRUE, FALSE)
	DIM negativeFlg = IIF(dec < 0, TRUE, FALSE)
	dec = ABS(dec)
	DIM integer = IIF(decimalFlg, COPY(dec, 1, POS(".", dec) - 1), dec)
	DIM offset = POWER(10, LENGTH(dec) - POS(".", dec))
	DIM decimal = IIF(decimalFlg, COPY(dec, POS(".", dec) + 1) / offset, 0)
	REPEAT
		hex = array[integer MOD 16] + hex
		integer = INT(integer / 16)
	UNTIL integer = 0
	IFB decimalFlg THEN
		hex = hex + "."
		DIM loop = 0
		REPEAT
			loop = loop + 1
			decimal = decimal * 16
			hex = hex + array[decimal]
			offset = POWER(10, LENGTH(decimal) - POS(".", decimal))
			decimal = (decimal * offset - INT(decimal) * offset) / offset
		UNTIL decimal = 0 OR loop >= 4
	ENDIF
	DIM bin = hexToBin(hex)
	DIM keta = LENGTH(bin)
	IFB keta < 8 THEN
		bin = strPad(bin, decimalFlg + 8, "0", LEFT)
	ELSEIF keta < 16 THEN
		bin = strPad(bin, decimalFlg + 16, "0", LEFT)
	ELSEIF keta < 24 THEN
		bin = strPad(bin, decimalFlg + 24, "0", LEFT)
	ELSEIF keta < 32 THEN
		bin = strPad(bin, decimalFlg + 32, "0", LEFT)
	ELSE
		bin = strPad(bin, decimalFlg + 64, "0", LEFT)
	ENDIF
	IFB negativeFlg THEN
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg, POS(".", bin) - LENGTH(bin), 0)
		DIM a = hexToDec(hex, FALSE)
		DIM b = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			b = b + POWER(2, i)
		NEXT
		dec = bitXor(a, b) + POWER(2, lsb)
		hex = decToHex(dec, signFlg, recursive + 1)
	ENDIF
	IFB recursive = 1 THEN
		DIM bit = COPY(hexToBin(COPY(hex, 1, 1)), 1, 1)
		DIM len = LENGTH(REPLACE(hex, ".", ""))
		IF negativeFlg AND (bit = "0" OR len MOD 2 <> 0) THEN hex = strRepeat("F", IIF(len MOD 2 <> 0, 1, 2)) + hex
		IF !negativeFlg AND signFlg AND (bit = "1" OR len MOD 2 <> 0) THEN hex = strRepeat("0", IIF(len MOD 2 <> 0, 1, 2)) + hex
	ENDIF
	RESULT = hex
FEND

//////////////////////////////////////////////////
// 【引数】
//   hex : 16進数 
// 【戻り値】
//   2進数に変換した値 
//////////////////////////////////////////////////
FUNCTION hexToBin(hex)
	HASHTBL hb
	hb["0"] = "0000";	hb["1"] = "0001";	hb["2"] = "0010";	hb["3"] = "0011";
	hb["4"] = "0100";	hb["5"] = "0101";	hb["6"] = "0110";	hb["7"] = "0111";
	hb["8"] = "1000";	hb["9"] = "1001";	hb["A"] = "1010";	hb["B"] = "1011";
	hb["C"] = "1100";	hb["D"] = "1101";	hb["E"] = "1110";	hb["F"] = "1111";
	DIM bin = ""
	IFB POS(".", hex) <> 0 THEN
		FOR i = 1 TO LENGTH(hex)
			DIM str = COPY(hex, i, 1)
			IF str = "." THEN bin = bin + "."
			bin = bin + hb[str]
		NEXT
	ELSE
		FOR i = 1 TO LENGTH(hex)
			bin = bin + hb[COPY(hex, i, 1)]
		NEXT
	ENDIF
	RESULT = bin
FEND

//////////////////////////////////////////////////
// 【引数】
//   hex : 16進数 
//   signFlg : 符号付きならばTrue 
// 【戻り値】
//   10進数に変換した値 
//////////////////////////////////////////////////
FUNCTION hexToDec(hex, signFlg = TRUE)
	hex = STRCONV(hex, SC_UPPERCASE)
	DIM dec = 0
	DIM decimalFlg = IIF(POS(".", hex) <> 0, TRUE, FALSE)
	hex = IIF(LENGTH(REPLACE(hex,".", "" )) MOD 2 <> 0, "0", "") + hex
	DIM negativeFlg = IIF(COPY(hexToBin(hex), 1, 1) = "1", TRUE, FALSE)
	DIM sign = 1
	IF negativeFlg AND signFlg THEN sign = -1
	IFB negativeFlg AND signFlg THEN
		DIM bin = hexToBin(hex)
		DIM msb = IIF(decimalFlg, POS(".", bin) - 1, LENGTH(bin))
		DIM lsb = IIF(decimalFlg, POS(".", bin) - LENGTH(bin), 0)
		DIM a = hexToDec(hex, FALSE)
		DIM b = POWER(2, msb) - 1
		FOR i = -1 TO lsb STEP -1
			b = b + POWER(2, i)
		NEXT
		DIM dec2 = bitXor(a, b) + POWER(2, lsb)
		hex = decToHex(dec2)
	ENDIF
	integer = IIF(decimalFlg, COPY(hex, 1, POS(".", hex) - 1), hex)
	decimal = IIF(decimalFlg, COPY(hex, POS(".", hex) + 1), "0")
	FOR i = 1 TO LENGTH(integer)
		s = COPY(hex, i, 1)
		num = IIF(CHKNUM(s), s, ASC(s) - (ASC("A") - 10))
		dec = dec + num * POWER(16, LENGTH(integer) - i)
	NEXT
	FOR i = 1 TO LENGTH(decimal)
		s = COPY(decimal, i, 1)
		num = IIF(CHKNUM(s), s, ASC(s) - (ASC("A") - 10))
		dec = dec + num * POWER(16, -1 * i)
	NEXT
	RESULT = sign * dec
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   r : 赤成分もしくは#RRGGBB形式の文字列 
//   g : 緑成分。rで#RRGGBB形式の文字列を指定した場合は省略。 
//   b : 青成分。rで#RRGGBB形式の文字列を指定した場合は省略。 
// 【戻り値】
//   Color値 
//////////////////////////////////////////////////
FUNCTION RGBToColor(r, g = EMPTY, b = EMPTY)
	IFB COPY(r, 1, 1) = "#" THEN
		DIM color = 0
		FOR i = 1 TO 3
			color = color + VAL(hexToDec(COPY(r, i * 2, 2), FALSE)) * POWER(256, i - 1)
		NEXT
		RESULT =  color
	ELSE
		RESULT = r + g * 256 + b * 65536
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   num : 符号を求める数値 
// 【戻り値】
//   1 : 正の数、0 : ゼロ、-1 : 負の数、ERR_VALUE : それ以外 
//////////////////////////////////////////////////
FUNCTION sign(num)
	SELECT TRUE
		CASE !CHKNUM(num)
			RESULT = ERR_VALUE
		CASE num > 0
			RESULT = 1
		CASE num = 0
			RESULT = 0
		CASE num < 0
			RESULT = -1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   input : 入力文字列 
//   length : 埋めたあとの長さ 
//   str : 埋める文字 
//   type : 埋める方向 
// 【戻り値】
//   指定文字で埋めた文字列 
//////////////////////////////////////////////////
FUNCTION strPad(input, length, str = " ", type = RIGHT)
	DIM s = ""
	SELECT type
		CASE LEFT
			FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
				s = s + str
			NEXT
			input = COPY(s, 1, length - LENGTH(input)) + input
		CASE RIGHT
			FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
				s = s + str
			NEXT
			input = input + COPY(s, 1, length - LENGTH(input))
	SELEND
	RESULT = input
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   uwscTime : UWSC時間 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION uwscToSerial(uwscTime)
	uwscDate = uwscTime / 86400
	RESULT = 36526 + uwscDate
FEND
使用関数

関連記事

PEEKCOLOR関数 (スクリプト関数)
PEEKCOLOR関数は、指定座標の色を取得する関数です。
invertedColor関数 (自作関数)
RGBの各成分、Color値、もしくは#RRGGBB形式の文字列から反転色を返します。返り値は引数と同じ形式で返します。
colorToRGB関数 (自作関数)
Color値を赤成分、緑成分、青成分に分解し格納した配列で返します。各成分は10進数の値で表されます。flgTrueを指定した場合、#RRGGBBの形式で返します。
CHKIMG関数 (スクリプト関数)
指定画像が画面上にあるかチェックしあればその情報を返します。
complementaryColor関数 (自作関数)
RGBの各成分、Color値、もしくは#RRGGBB形式の文字列から補色を返します。返り値は引数と同じ形式で返します。
BGRToColor関数 (自作関数)
引数に指定された青成分、緑成分、赤成分、もしくは#BBGGRR形式の文字列からColor値を求めます。
Borders.Color プロパティ (Excel)
罫線の色を取得または設定する。
colorToBGR関数 (自作関数)
Color値を青成分、緑成分、赤成分に分解し格納した配列で返します。各成分は10進数の値で表されます。flgTrueを指定した場合、#BBGGRRの形式で返します。