我在从C#中的colordialog框中获取颜色名称时遇到问题。我现在有以下代码来完成此任务:
-(void)uploadFile
{
//-------code for uploading data to icloud
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject]
NSString *str=[NSString stringWithFormat:@"%@/XMPPMessageArchiving.sqlite",applicationSupportDirectory]; //copy this file
NSString *fileName = [NSString stringWithFormat:@"XMPPMessageArchiving.sqlite"];
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *fromURL = [NSURL fileURLWithPath:str];
NSURL *toURL = [[ubiq URLByAppendingPathComponent:@"Documents"]
URLByAppendingPathComponent:fileName];;
[coordinator coordinateReadingItemAtURL:fromURL options:0 writingItemAtURL:toURL options:NSFileCoordinatorWritingForReplacing error:nil byAccessor:^(NSURL *newReadingURL, NSURL *newWritingURL)
{
NSError *error=nil;
NSData *data = [[NSFileManager defaultManager] contentsAtPath:fromURL.path];
//if file is already present on icloud replace that file with new file
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:newWritingURL.path];
if (fileExists)
{
[[NSFileManager defaultManager] removeItemAtPath:toURL.path error:NULL];
}
BOOL isCopied= [[NSFileManager defaultManager] copyItemAtPath:newReadingURL.path toPath:newWritingURL.path error:nil];
NSLog(@"file copied= %d",isCopied);
}];
}
正如您所看到的,我使用'C.Name.ToString'来获取颜色名称,但不知何故,这会返回颜色代码,例如:f0018000。我需要的是颜色的名称,例如:RED或BLUE。
我需要这个的原因是因为我将颜色存储在 private void bPickColor_Click(object sender, EventArgs e)
{
ColorDialog colorDlg = new ColorDialog();
colorDlg.AllowFullOpen = false;
colorDlg.AnyColor = true;
colorDlg.SolidColorOnly = false;
colorDlg.Color = Color.Red;
if (colorDlg.ShowDialog() == DialogResult.OK)
{
//ColorConverter conv = new ColorConverter();
Color c = colorDlg.Color;
//string s = conv.ConvertToString(c);
//string h = Conversion.Hex(s.ToArgb);
_ColorName = c.Name.ToString();
MessageBox.Show(_ColorName);
bPickColor.BackColor = colorDlg.Color;
}
}
中并使用以下代码读取它们:
datatable
所以我到目前为止尝试的是搜索可以根据颜色代码获取颜色名称的代码,但是我发现的代码片段不起作用。
现在我还尝试用相应的颜色名称制作所有颜色代码的字典,但我也无法使其工作。
有没有人能解决这个问题?如何获得颜色的名称(蓝色,红色)?
答案 0 :(得分:0)
试试这个
_ColorName = c.Name.ToString();
Color myColor = ColorTranslator.FromHtml(_ColorName);
MessageBox.Show(myColor.Name);
答案 1 :(得分:0)
我找到了解决方案。我在这里找到了这段代码(system.Linq需要使这段代码工作):convert hex code to color name:
string GetColorName(Color color)
{
var colorProperties = typeof(Color)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where(p => p.PropertyType == typeof(Color));
foreach (var colorProperty in colorProperties)
{
var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
if (colorPropertyValue.R == color.R
&& colorPropertyValue.G == color.G
&& colorPropertyValue.B == color.B)
{
return colorPropertyValue.Name;
}
}
//If unknown color, fallback to the hex value
//(or you could return null, "Unkown" or whatever you want)
return ColorTranslator.ToHtml(color);
}
在colordialog选择器中,我现在使用此代码:
private void bPickColor_Click(object sender, EventArgs e)
{
ColorDialog colorDlg = new ColorDialog();
colorDlg.AllowFullOpen = false;
colorDlg.AnyColor = true;
colorDlg.SolidColorOnly = false;
colorDlg.Color = Color.Red;
if (colorDlg.ShowDialog() == DialogResult.OK)
{
Color c = colorDlg.Color;
_ColorName = GetColorName(c);
MessageBox.Show(_ColorName);
bPickColor.BackColor = colorDlg.Color;
}
}
这种方式完美无缺。虽然我确实不知道所有颜色是否都有相应的名称,但我仍然可以在代码中处理它。 谢谢你的所有答案!
答案 2 :(得分:0)
检查一下: https://msdn.microsoft.com/en-us/library/aa358802.aspx
这些是可能的颜色名称。 因此,如果您必须存储颜色名,请在这些选项中进行选择! ; - )
以下是如何制作所需词典的示例:
echo $a | cut -d'-' -f3
答案 3 :(得分:0)