public static int RegisterColor(string name, Color color) { name = name.ToLower(); int index = c_names.Count; c_indices.Add(name, index); // 重複があれば茲で例外 c_names.Add(name); c_values.Add(color); return(index); }
//=========================================================== // 色の読み取り //=========================================================== /// <summary> /// 文字列を読み取って ColorName のインスタンスを作成します。 /// </summary> /// <param name="text">色の情報を格納している文字列を指定します。</param> /// <param name="index">色の情報の開始位置を指定します。</param> public ColorName(string text, ref int index) { int i = index; char c; afh.Parse.LetterType lt; // 空白読み飛ばし do { if (i >= text.Length) { throw new System.FormatException("有効な文字が含まれていません。"); } lt = afh.Parse.LetterType.GetLetterType(c = text[i++]); }while(lt.IsInvalid || lt.IsSpace); // [文字: 初めの非空白文字] // [位置: "文字" の次] if (c == '#') { if (!read_sharp(text, ref i, out this.color)) { throw new System.FormatException("色を表現する文字列の形式が誤っています。"); } this.hasName = false; index = i; } else { string name; // 名前の取得 while (true) { // next if (i < text.Length) { lt = afh.Parse.LetterType.GetLetterType(c = text[i]); } else { c = '\0'; name = text.Substring(index, i - index).ToLower(); break; } // 抜け判定 if (c == '(' || lt.IsSpace || lt.IsInvalid) { name = text.Substring(index, i - index).ToLower(); // 空白飛ばし while ((lt.IsInvalid || lt.IsSpace) && ++i < text.Length) { lt = afh.Parse.LetterType.GetLetterType(c = text[i]); } break; } i++; } // [文字: "名前" 後の初めの '非スペース文字'|'null 終端'] // [位置: "文字"] if (c == '(') { this.hasName = false; double[] args = read_func_args(text, ref i); switch (name) { case "rgb": this.color = (int)Color.FromRgb( read_double2byte(args[0]), read_double2byte(args[1]), read_double2byte(args[2]) ); break; case "argb": this.color = (int)Color.FromArgb( read_double2byte(args[0]), read_double2byte(args[1]), read_double2byte(args[2]), read_double2byte(args[3]) ); break; case "cmy": this.color = (int)Color.FromCmy( read_double2byte(args[0]), read_double2byte(args[1]), read_double2byte(args[2]) ); break; case "hsv": this.color = (int)Color.FromHsv( args[0], read_double2byte(args[1]), read_double2byte(args[2]) ); break; default: throw new System.FormatException("'" + name + "' という色関数は登録されていません"); } } else { this.hasName = true; if (!c_indices.TryGetValue(name, out this.color)) { throw new System.FormatException("'" + name + "' という色は登録されていません"); } } index = i; } }