开发者

Hex Pair to Hex Shorthand

开发者 https://www.devze.com 2023-01-02 13:41 出处:网络
I have a bunch of Color objects (.Net). I want to convert them to Hex, which is simple enough with something like:

I have a bunch of Color objects (.Net). I want to convert them to Hex, which is simple enough with something like:

Dim clr As Color = Color.FromArgb(255, 0, 0)
Dim clrString = ColorTranslator.ToH开发者_开发技巧tml(clr)

Is there a way in .Net or through RegEx (or some other way) that I can determine if Hex shorthand (like #F00) is available for the specified Color and then convert it to that? So for colors that can have a Hex shorthand, convert to that, otherwise, convert to Hex Pair #FF0000.


^#([0-9A-F])\1([0-9A-F])\2([0-9A-F])\3$

This uses 3 back-references to check that each hex digit is followed by a copy. So anything with the pattern #xxyyzz (which can be converted to #xyz) matches.


This link describes how the Shorthand Hex Notation works.

Shorthand Hex Notation

So, in theory, any implementation that will allow you to analyze a Hex RGB value and detect "duplicate double" character values should be able to reduce it to a Hex Shorthand.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消