Convert HTML color to OLEHow to simplify these delegate functions?Binary converter and sufficient testsAssigning a value to a class variableConstructing query conditions from answers that may be nullRemoving doubles from a stringRectangle ClassGenerate URLs, Download, extract and add filesCaching an integer - distinguishing no value from zeroUse filename to determine file typeCreate HTML structure dynamically
Maximum number of pairwise linearly independent vectors
Default camera device to show screen instead of physical camera
How to think about joining a company whose business I do not understand?
Why the color Red in Us, what is the significance?
Is this kind of description not recommended?
"Silverware", "Tableware", and "Dishes"
Stuffing in the middle
How big would a Daddy Longlegs Spider need to be to kill an average Human?
Earliest evidence of objects intended for future archaeologists?
Unsolved Problems (Not Independent of ZFC) due to Lack of Computational Power
How does the Saturn V Dynamic Test Stand work?
Is "stainless" a bulk or a surface property of stainless steel?
Writing/buying Seforim rather than Sefer Torah
Does the Symbiotic Entity damage apply to a creature hit by the secondary damage of Green Flame Blade?
Sous vide chicken without an internal temperature of 165
I think my coworker went through my notebook and took my project ideas
Using は before 欲しい instead が
TechSupport Issue ID#812
Can I submit a paper under an alias so as to avoid trouble in my country?
Why don't sharp and flat root note chords seem to be present in much guitar music?
Unity: transform.LookAt(target) not "looking at" target?
Convert HTML color to OLE
Count the frequency of items in an array
Are there any OR challenges that are similar to kaggle's competitions?
Convert HTML color to OLE
How to simplify these delegate functions?Binary converter and sufficient testsAssigning a value to a class variableConstructing query conditions from answers that may be nullRemoving doubles from a stringRectangle ClassGenerate URLs, Download, extract and add filesCaching an integer - distinguishing no value from zeroUse filename to determine file typeCreate HTML structure dynamically
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have this function that takes a string which represents a html-color and returns the OLE value for it. If the string is null or empty or it can't be parsed, it uses a default value.
However I have to write that one line containing the default value twice and I don't see how I could remove this redundancy in the best possible way.
I know this isn't a big deal but I am curious to see what you guys might suggest for this :)
What do you think is the best possible way to remove this redundancy?
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = Color.FromArgb(224, 224, 224);
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = Color.FromArgb(224, 224, 224);
return ColorTranslator.ToOle(c);
c# beginner converting
$endgroup$
add a comment |
$begingroup$
I have this function that takes a string which represents a html-color and returns the OLE value for it. If the string is null or empty or it can't be parsed, it uses a default value.
However I have to write that one line containing the default value twice and I don't see how I could remove this redundancy in the best possible way.
I know this isn't a big deal but I am curious to see what you guys might suggest for this :)
What do you think is the best possible way to remove this redundancy?
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = Color.FromArgb(224, 224, 224);
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = Color.FromArgb(224, 224, 224);
return ColorTranslator.ToOle(c);
c# beginner converting
$endgroup$
add a comment |
$begingroup$
I have this function that takes a string which represents a html-color and returns the OLE value for it. If the string is null or empty or it can't be parsed, it uses a default value.
However I have to write that one line containing the default value twice and I don't see how I could remove this redundancy in the best possible way.
I know this isn't a big deal but I am curious to see what you guys might suggest for this :)
What do you think is the best possible way to remove this redundancy?
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = Color.FromArgb(224, 224, 224);
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = Color.FromArgb(224, 224, 224);
return ColorTranslator.ToOle(c);
c# beginner converting
$endgroup$
I have this function that takes a string which represents a html-color and returns the OLE value for it. If the string is null or empty or it can't be parsed, it uses a default value.
However I have to write that one line containing the default value twice and I don't see how I could remove this redundancy in the best possible way.
I know this isn't a big deal but I am curious to see what you guys might suggest for this :)
What do you think is the best possible way to remove this redundancy?
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = Color.FromArgb(224, 224, 224);
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = Color.FromArgb(224, 224, 224);
return ColorTranslator.ToOle(c);
c# beginner converting
c# beginner converting
edited 9 hours ago
t3chb0t
37.4k7 gold badges60 silver badges141 bronze badges
37.4k7 gold badges60 silver badges141 bronze badges
asked 9 hours ago
JoeliusJoelius
1615 bronze badges
1615 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Since a method has to be executed to get this color, you cannot declare the color as constant. A static readonly field is what comes closest to a const.
private static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = DefaultColor
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = DefaultColor
return ColorTranslator.ToOle(c);
Another approach would be to directly store the OLE color as int.
const int DefaultOleColor = 14737632; // R=224, G=224, B=224
private int GetOleFromHTML(string stringRep)
if (string.IsNullOrWhiteSpace(stringRep))
return DefaultOleColor;
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
Or calculated explicitly:
const int DefaultOleColor = 256 * (256 * 224 + 224) + 224;
Or with bit shift operations
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224; // My preferred variant.
These expressions can be used to initialize the constant as they can be fully evaluated at compile time.
You can test these variants easily in the Immediate Window of Visual Studio. You must qualify the names with the namespaces for the first variant (System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(224, 224, 224))).
Avoiding repetition is not the only reason for having constants. Constant declarations give a name to otherwise "magic" values. See Magic number (programming) (Wikipedia).
This variant avoids the repetition by restructuring the code, but still uses a constant.
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224;
private int GetOleFromHTML(string stringRep)
if (!String.IsNullOrWhiteSpace(stringRep))
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
$endgroup$
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
add a comment |
$begingroup$
Another option, though not without caveats, is to reverse the logic. Something like this should work:
private int GetOleFromHTML(string stringRep)
Color c = Color.FromArgb(224, 224, 224);
if (!string.IsNullOrWhiteSpace(stringRep))
try
c = ColorTranslator.FromHtml(stringRep);
catch
//Use default color value if the string is invalid.
return ColorTranslator.ToOle(c);
$endgroup$
add a comment |
$begingroup$
You have difficulties getting rid of the repeated code because you're trying to force two different operations into one:
- parsing html
- converting the result into
OLEcolor
Start with extracting the default value and make it a field:
static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
Then create the main API and give it a name that clearly communicates that it does. Now that the parsing has been extrated, you can as simple ternary operators here:
public Color ParseHtmlColorOrDefault(string value, Color defaultColor)
return
!string.IsNullOrWhiteSpace(value) && TryParseHtmlColor(value, out var color)
? color
: defaultColor;
The parsing with its try/catch as the TrySomething pattern:
private static bool TryParseHtmlColor(string value, out Color color)
try
color = ColorTranslator.FromHtml(value);
return true;
catch
color = Color.Empty;
return false;
Finally, create an extension for the final step of converting Color to OLE color:
public static int ToOle(this Color color) => ColorTranslator.ToOle(color);
Use it like that:
var oleColor = ParseHtmlColorOrDefault("#FFAABB", DefaultColor).ToOle();
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226427%2fconvert-html-color-to-ole%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Since a method has to be executed to get this color, you cannot declare the color as constant. A static readonly field is what comes closest to a const.
private static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = DefaultColor
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = DefaultColor
return ColorTranslator.ToOle(c);
Another approach would be to directly store the OLE color as int.
const int DefaultOleColor = 14737632; // R=224, G=224, B=224
private int GetOleFromHTML(string stringRep)
if (string.IsNullOrWhiteSpace(stringRep))
return DefaultOleColor;
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
Or calculated explicitly:
const int DefaultOleColor = 256 * (256 * 224 + 224) + 224;
Or with bit shift operations
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224; // My preferred variant.
These expressions can be used to initialize the constant as they can be fully evaluated at compile time.
You can test these variants easily in the Immediate Window of Visual Studio. You must qualify the names with the namespaces for the first variant (System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(224, 224, 224))).
Avoiding repetition is not the only reason for having constants. Constant declarations give a name to otherwise "magic" values. See Magic number (programming) (Wikipedia).
This variant avoids the repetition by restructuring the code, but still uses a constant.
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224;
private int GetOleFromHTML(string stringRep)
if (!String.IsNullOrWhiteSpace(stringRep))
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
$endgroup$
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
add a comment |
$begingroup$
Since a method has to be executed to get this color, you cannot declare the color as constant. A static readonly field is what comes closest to a const.
private static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = DefaultColor
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = DefaultColor
return ColorTranslator.ToOle(c);
Another approach would be to directly store the OLE color as int.
const int DefaultOleColor = 14737632; // R=224, G=224, B=224
private int GetOleFromHTML(string stringRep)
if (string.IsNullOrWhiteSpace(stringRep))
return DefaultOleColor;
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
Or calculated explicitly:
const int DefaultOleColor = 256 * (256 * 224 + 224) + 224;
Or with bit shift operations
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224; // My preferred variant.
These expressions can be used to initialize the constant as they can be fully evaluated at compile time.
You can test these variants easily in the Immediate Window of Visual Studio. You must qualify the names with the namespaces for the first variant (System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(224, 224, 224))).
Avoiding repetition is not the only reason for having constants. Constant declarations give a name to otherwise "magic" values. See Magic number (programming) (Wikipedia).
This variant avoids the repetition by restructuring the code, but still uses a constant.
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224;
private int GetOleFromHTML(string stringRep)
if (!String.IsNullOrWhiteSpace(stringRep))
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
$endgroup$
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
add a comment |
$begingroup$
Since a method has to be executed to get this color, you cannot declare the color as constant. A static readonly field is what comes closest to a const.
private static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = DefaultColor
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = DefaultColor
return ColorTranslator.ToOle(c);
Another approach would be to directly store the OLE color as int.
const int DefaultOleColor = 14737632; // R=224, G=224, B=224
private int GetOleFromHTML(string stringRep)
if (string.IsNullOrWhiteSpace(stringRep))
return DefaultOleColor;
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
Or calculated explicitly:
const int DefaultOleColor = 256 * (256 * 224 + 224) + 224;
Or with bit shift operations
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224; // My preferred variant.
These expressions can be used to initialize the constant as they can be fully evaluated at compile time.
You can test these variants easily in the Immediate Window of Visual Studio. You must qualify the names with the namespaces for the first variant (System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(224, 224, 224))).
Avoiding repetition is not the only reason for having constants. Constant declarations give a name to otherwise "magic" values. See Magic number (programming) (Wikipedia).
This variant avoids the repetition by restructuring the code, but still uses a constant.
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224;
private int GetOleFromHTML(string stringRep)
if (!String.IsNullOrWhiteSpace(stringRep))
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
$endgroup$
Since a method has to be executed to get this color, you cannot declare the color as constant. A static readonly field is what comes closest to a const.
private static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
private int GetOleFromHTML(string stringRep)
Color c;
if (string.IsNullOrWhiteSpace(stringRep))
c = DefaultColor
else
try
c = ColorTranslator.FromHtml(stringRep);
catch
c = DefaultColor
return ColorTranslator.ToOle(c);
Another approach would be to directly store the OLE color as int.
const int DefaultOleColor = 14737632; // R=224, G=224, B=224
private int GetOleFromHTML(string stringRep)
if (string.IsNullOrWhiteSpace(stringRep))
return DefaultOleColor;
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
Or calculated explicitly:
const int DefaultOleColor = 256 * (256 * 224 + 224) + 224;
Or with bit shift operations
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224; // My preferred variant.
These expressions can be used to initialize the constant as they can be fully evaluated at compile time.
You can test these variants easily in the Immediate Window of Visual Studio. You must qualify the names with the namespaces for the first variant (System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(224, 224, 224))).
Avoiding repetition is not the only reason for having constants. Constant declarations give a name to otherwise "magic" values. See Magic number (programming) (Wikipedia).
This variant avoids the repetition by restructuring the code, but still uses a constant.
const int DefaultOleColor = 224 << 16 | 224 << 8 | 224;
private int GetOleFromHTML(string stringRep)
if (!String.IsNullOrWhiteSpace(stringRep))
try
return ColorTranslator.ToOle(ColorTranslator.FromHtml(stringRep));
catch
return DefaultOleColor;
edited 8 hours ago
answered 9 hours ago
Olivier Jacot-DescombesOlivier Jacot-Descombes
2,84512 silver badges19 bronze badges
2,84512 silver badges19 bronze badges
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
add a comment |
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
1
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
$begingroup$
This answer keeps getting better with every edit :) You already have my vote. One question though: do you think the catch could somehow be prevented?
$endgroup$
– dfhwze
8 hours ago
1
1
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Here you can see the Reference Source of the ColorTranslator.FromHtml(String) Method. It is quite complex as it accounts for different formats of HTML color. An attempt to avoid these exceptions would be at least as complex. Assuming the exceptions occur rarely, it is not worth the candle.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
$begingroup$
Ah I was afraid it would be too complex. Thanks for this feedback.
$endgroup$
– dfhwze
8 hours ago
add a comment |
$begingroup$
Another option, though not without caveats, is to reverse the logic. Something like this should work:
private int GetOleFromHTML(string stringRep)
Color c = Color.FromArgb(224, 224, 224);
if (!string.IsNullOrWhiteSpace(stringRep))
try
c = ColorTranslator.FromHtml(stringRep);
catch
//Use default color value if the string is invalid.
return ColorTranslator.ToOle(c);
$endgroup$
add a comment |
$begingroup$
Another option, though not without caveats, is to reverse the logic. Something like this should work:
private int GetOleFromHTML(string stringRep)
Color c = Color.FromArgb(224, 224, 224);
if (!string.IsNullOrWhiteSpace(stringRep))
try
c = ColorTranslator.FromHtml(stringRep);
catch
//Use default color value if the string is invalid.
return ColorTranslator.ToOle(c);
$endgroup$
add a comment |
$begingroup$
Another option, though not without caveats, is to reverse the logic. Something like this should work:
private int GetOleFromHTML(string stringRep)
Color c = Color.FromArgb(224, 224, 224);
if (!string.IsNullOrWhiteSpace(stringRep))
try
c = ColorTranslator.FromHtml(stringRep);
catch
//Use default color value if the string is invalid.
return ColorTranslator.ToOle(c);
$endgroup$
Another option, though not without caveats, is to reverse the logic. Something like this should work:
private int GetOleFromHTML(string stringRep)
Color c = Color.FromArgb(224, 224, 224);
if (!string.IsNullOrWhiteSpace(stringRep))
try
c = ColorTranslator.FromHtml(stringRep);
catch
//Use default color value if the string is invalid.
return ColorTranslator.ToOle(c);
edited 7 hours ago
answered 8 hours ago
tinstaafltinstaafl
7,4871 gold badge9 silver badges31 bronze badges
7,4871 gold badge9 silver badges31 bronze badges
add a comment |
add a comment |
$begingroup$
You have difficulties getting rid of the repeated code because you're trying to force two different operations into one:
- parsing html
- converting the result into
OLEcolor
Start with extracting the default value and make it a field:
static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
Then create the main API and give it a name that clearly communicates that it does. Now that the parsing has been extrated, you can as simple ternary operators here:
public Color ParseHtmlColorOrDefault(string value, Color defaultColor)
return
!string.IsNullOrWhiteSpace(value) && TryParseHtmlColor(value, out var color)
? color
: defaultColor;
The parsing with its try/catch as the TrySomething pattern:
private static bool TryParseHtmlColor(string value, out Color color)
try
color = ColorTranslator.FromHtml(value);
return true;
catch
color = Color.Empty;
return false;
Finally, create an extension for the final step of converting Color to OLE color:
public static int ToOle(this Color color) => ColorTranslator.ToOle(color);
Use it like that:
var oleColor = ParseHtmlColorOrDefault("#FFAABB", DefaultColor).ToOle();
$endgroup$
add a comment |
$begingroup$
You have difficulties getting rid of the repeated code because you're trying to force two different operations into one:
- parsing html
- converting the result into
OLEcolor
Start with extracting the default value and make it a field:
static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
Then create the main API and give it a name that clearly communicates that it does. Now that the parsing has been extrated, you can as simple ternary operators here:
public Color ParseHtmlColorOrDefault(string value, Color defaultColor)
return
!string.IsNullOrWhiteSpace(value) && TryParseHtmlColor(value, out var color)
? color
: defaultColor;
The parsing with its try/catch as the TrySomething pattern:
private static bool TryParseHtmlColor(string value, out Color color)
try
color = ColorTranslator.FromHtml(value);
return true;
catch
color = Color.Empty;
return false;
Finally, create an extension for the final step of converting Color to OLE color:
public static int ToOle(this Color color) => ColorTranslator.ToOle(color);
Use it like that:
var oleColor = ParseHtmlColorOrDefault("#FFAABB", DefaultColor).ToOle();
$endgroup$
add a comment |
$begingroup$
You have difficulties getting rid of the repeated code because you're trying to force two different operations into one:
- parsing html
- converting the result into
OLEcolor
Start with extracting the default value and make it a field:
static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
Then create the main API and give it a name that clearly communicates that it does. Now that the parsing has been extrated, you can as simple ternary operators here:
public Color ParseHtmlColorOrDefault(string value, Color defaultColor)
return
!string.IsNullOrWhiteSpace(value) && TryParseHtmlColor(value, out var color)
? color
: defaultColor;
The parsing with its try/catch as the TrySomething pattern:
private static bool TryParseHtmlColor(string value, out Color color)
try
color = ColorTranslator.FromHtml(value);
return true;
catch
color = Color.Empty;
return false;
Finally, create an extension for the final step of converting Color to OLE color:
public static int ToOle(this Color color) => ColorTranslator.ToOle(color);
Use it like that:
var oleColor = ParseHtmlColorOrDefault("#FFAABB", DefaultColor).ToOle();
$endgroup$
You have difficulties getting rid of the repeated code because you're trying to force two different operations into one:
- parsing html
- converting the result into
OLEcolor
Start with extracting the default value and make it a field:
static readonly Color DefaultColor = Color.FromArgb(224, 224, 224);
Then create the main API and give it a name that clearly communicates that it does. Now that the parsing has been extrated, you can as simple ternary operators here:
public Color ParseHtmlColorOrDefault(string value, Color defaultColor)
return
!string.IsNullOrWhiteSpace(value) && TryParseHtmlColor(value, out var color)
? color
: defaultColor;
The parsing with its try/catch as the TrySomething pattern:
private static bool TryParseHtmlColor(string value, out Color color)
try
color = ColorTranslator.FromHtml(value);
return true;
catch
color = Color.Empty;
return false;
Finally, create an extension for the final step of converting Color to OLE color:
public static int ToOle(this Color color) => ColorTranslator.ToOle(color);
Use it like that:
var oleColor = ParseHtmlColorOrDefault("#FFAABB", DefaultColor).ToOle();
answered 6 hours ago
t3chb0tt3chb0t
37.4k7 gold badges60 silver badges141 bronze badges
37.4k7 gold badges60 silver badges141 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226427%2fconvert-html-color-to-ole%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown