Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Color challenge Pin
Eddy Vluggen1-Jul-17 0:31
professionalEddy Vluggen1-Jul-17 0:31 
GeneralRe: C# Color challenge Pin
BillWoodruff2-Jul-17 14:53
professionalBillWoodruff2-Jul-17 14:53 
GeneralRe: C# Color challenge Pin
Eddy Vluggen3-Jul-17 23:30
professionalEddy Vluggen3-Jul-17 23:30 
GeneralRe: C# Color challenge Pin
harold aptroot2-Jul-17 2:34
harold aptroot2-Jul-17 2:34 
GeneralRe: C# Color challenge Pin
BillWoodruff2-Jul-17 14:35
professionalBillWoodruff2-Jul-17 14:35 
GeneralRe: C# Color challenge Pin
harold aptroot2-Jul-17 15:07
harold aptroot2-Jul-17 15:07 
GeneralRe: C# Color challenge Pin
BillWoodruff2-Jul-17 19:21
professionalBillWoodruff2-Jul-17 19:21 
GeneralRe: C# Color challenge Pin
Richard Deeming3-Jul-17 1:35
mveRichard Deeming3-Jul-17 1:35 
Are you trying to serialize the Color? That's the only time I can think of that you'd need to do this.

The obvious solution would be to combine IsKnownColor[^], ToKnownColor[^] and FromKnownColor[^]; and also IsNamedColor[^], Name[^] and FromName[^].
C#
public struct ColorDetails : IEquatable<ColorDetails>
{
    private ColorDetails(KnownColor knownColor, string name, int argb)
    {
        KnownColor = knownColor;
        Name = name;
        Argb = argb;
    }
    
    public KnownColor KnownColor { get; }
    public string Name { get; }
    public int Argb { get; }
    
    public static ColorDetails FromColor(Color color)
    {
        if (color.IsKnownColor)
        {
            return new ColorDetails(color.ToKnownColor(), null, color.ToArgb());
        }
        if (color.IsNamedColor)
        {
            return new ColorDetails(0, color.Name, color.ToArgb());
        }
        
        return new ColorDetails(0, null, color.ToArgb());
    }
    
    public Color ToColor()
    {
        if (KnownColor != 0)
        {
            return Color.FromKnownColor(KnownColor);
        }
        if (Name != null)
        {
            return Color.FromName(Name);
        }
        
        return Color.FromArgb(Argb);
    }
    
    public override int GetHashCode()
    {
        return Argb;
    }
    
    public override bool Equals(object obj)
    {
        return obj is ColorDetails && Equals((ColorDetails)obj);
    }
    
    public bool Equals(ColorDetails other)
    {
        return Argb == other.Argb;
    }
    
    public static bool operator ==(ColorDetails left, ColorDetails right)
    {
        return left.Equals(right);
    }
    
    public static bool operator !=(ColorDetails left, ColorDetails right)
    {
        return !left.Equals(right);
    }
}

C#
ColorDetails cd = ColorDetails.FromColor(Color.Red);
Color clr = cd.ToColor();

bool ismatchEq1 = Color.Red.Equals(clr); // True

bool ismatchEq2 = Color.Red == clr; // True

bool isknown = clr.IsKnownColor; // True
    
bool isnamed  = clr.IsNamedColor; // True




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: C# Color challenge Pin
BillWoodruff3-Jul-17 2:51
professionalBillWoodruff3-Jul-17 2:51 
Questionprinting an string data. Pin
rahul199529-Jun-17 20:21
rahul199529-Jun-17 20:21 
AnswerRe: printing an string data. Pin
OriginalGriff29-Jun-17 20:34
mveOriginalGriff29-Jun-17 20:34 
AnswerRe: printing an string data. Pin
Richard MacCutchan29-Jun-17 21:36
mveRichard MacCutchan29-Jun-17 21:36 
AnswerRe: printing an string data. Pin
bindumol L29-Jun-17 21:53
bindumol L29-Jun-17 21:53 
SuggestionRe: printing an string data. Pin
Richard MacCutchan29-Jun-17 23:07
mveRichard MacCutchan29-Jun-17 23:07 
Questionqr code printing. Pin
rahul199528-Jun-17 0:31
rahul199528-Jun-17 0:31 
AnswerRe: qr code printing. Pin
Pete O'Hanlon28-Jun-17 0:35
mvePete O'Hanlon28-Jun-17 0:35 
Questionhow we can adjust the size of qr code while printing? Pin
rahul199527-Jun-17 19:07
rahul199527-Jun-17 19:07 
AnswerRe: how we can adjust the size of qr code while printing? Pin
OriginalGriff27-Jun-17 19:51
mveOriginalGriff27-Jun-17 19:51 
QuestionHow to Check Data for data existence in database using C# Pin
Tej_dev27-Jun-17 8:23
Tej_dev27-Jun-17 8:23 
AnswerRe: How to Check Data for data existence in database using C# Pin
Mycroft Holmes27-Jun-17 19:32
professionalMycroft Holmes27-Jun-17 19:32 
AnswerRe: How to Check Data for data existence in database using C# Pin
jschell28-Jun-17 6:03
jschell28-Jun-17 6:03 
GeneralRe: How to Check Data for data existence in database using C# Pin
Tej_dev28-Jun-17 9:20
Tej_dev28-Jun-17 9:20 
GeneralRe: How to Check Data for data existence in database using C# Pin
Pete O'Hanlon28-Jun-17 10:44
mvePete O'Hanlon28-Jun-17 10:44 
GeneralRe: How to Check Data for data existence in database using C# Pin
Tej_dev29-Jun-17 4:51
Tej_dev29-Jun-17 4:51 
GeneralRe: How to Check Data for data existence in database using C# Pin
Pete O'Hanlon29-Jun-17 4:55
mvePete O'Hanlon29-Jun-17 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.