System.Drawing.Color를 RGB 및 16 진수 값으로 변환
C #을 사용하여 다음 두 가지를 개발하려고했습니다. 내가하고있는 방식에 문제가있을 수 있으며 친절한 조언이 필요합니다. 또한 기존 방법이 동일한 지 여부를 알 수 없습니다.
private static String HexConverter(System.Drawing.Color c)
{
String rtn = String.Empty;
try
{
rtn = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
catch (Exception ex)
{
//doing nothing
}
return rtn;
}
private static String RGBConverter(System.Drawing.Color c)
{
String rtn = String.Empty;
try
{
rtn = "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")";
}
catch (Exception ex)
{
//doing nothing
}
return rtn;
}
감사.
여기에서 문제를 보지 못했습니다. 코드가 나에게 좋아 보인다.
내가 생각할 수있는 유일한 방법입니다 시도 / catch 블록 중복 있음 - C는 null 일 수 없습니다 그래서, 색상, G 구조체 및 R이고, B는 바이트 c.R.ToString()
, c.G.ToString()
그리고 c.B.ToString()
실제로 실패 할 수 없습니다합니다 ( 내가 실패한 것을 볼 수있는 유일한 방법은입니다 NullReferenceException
. 실제로는 null이 될 수 없습니다).
다음을 사용하여 모든 것을 정리할 수 있습니다.
private static String HexConverter(System.Drawing.Color c)
{
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
private static String RGBConverter(System.Drawing.Color c)
{
return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")";
}
간단하게 유지하고 기본 색상 번역기를 사용할 수 있습니다.
Color red = ColorTranslator.FromHtml("#FF0000");
string redHex = ColorTranslator.ToHtml(red);
그런 다음 세 가지 색상 쌍을 정수 형식으로 분리하십시오.
int value = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
If you can use C#6, you can benefit from Interpolated Strings and rewrite @Ari Roth's solution like this:
C# 6 :
public static class ColorConverterExtensions
{
public static string ToHexString(this Color c) => $"#{c.R:X2}{c.G:X2}{c.B:X2}";
public static string ToRgbString(this Color c) => $"RGB({c.R}, {c.G}, {c.B})";
}
Also:
- I add the keyword
this
to use them as extensions methods. - We can use the type keyword
string
instead of the class name. - We can use lambda syntax.
- I rename them to be more explicit for my taste.
e.g.
ColorTranslator.ToHtml(Color.FromArgb(Color.Tomato.ToArgb()))
This can avoid the KnownColor trick.
I found an extension method that works quite well
public static string ToHex(this Color color)
{
return String.Format("#{0}{1}{2}{3}"
, color.A.ToString("X").Length == 1 ? String.Format("0{0}", color.A.ToString("X")) : color.A.ToString("X")
, color.R.ToString("X").Length == 1 ? String.Format("0{0}", color.R.ToString("X")) : color.R.ToString("X")
, color.G.ToString("X").Length == 1 ? String.Format("0{0}", color.G.ToString("X")) : color.G.ToString("X")
, color.B.ToString("X").Length == 1 ? String.Format("0{0}", color.B.ToString("X")) : color.B.ToString("X"));
}
For hexadecimal code try this
- Get ARGB (Alpha, Red, Green, Blue) representation for the color
- Filter out Alpha channel:
& 0x00FFFFFF
- Format out the value (as hexadecimal "X6" for hex)
For RGB one
- Just format out
Red
,Green
,Blue
values
Implementation
private static string HexConverter(Color c) {
return String.Format("#{0:X6}", c.ToArgb() & 0x00FFFFFF);
}
public static string RgbConverter(Color c) {
return String.Format("RGB({0},{1},{2})", c.R, c.G, c.B);
}
참고URL : https://stackoverflow.com/questions/2395438/convert-system-drawing-color-to-rgb-and-hex-value
'programing tip' 카테고리의 다른 글
EditText maxLines가 작동하지 않습니다-사용자는 여전히 설정보다 많은 라인을 입력 할 수 있습니다 (0) | 2020.07.22 |
---|---|
보고서를 작성할 때 "보고서 데이터"창이 표시되지 않는 이유는 무엇입니까? (0) | 2020.07.22 |
스위프트 : 열거 형 값을 문자열로 변환 하시겠습니까? (0) | 2020.07.22 |
JavaScript를 사용하여 요소를 어떻게 스크롤합니까? (0) | 2020.07.22 |
android : DrawableRight를 사용하여 버튼 및 TextViews에서 VectorDrawable을 사용할 수 있습니까? (0) | 2020.07.22 |