String to html color

Convert color to/from HTML

This unit has functions which convert TColor value to/from HTML color string: #rrggbb. It can convert HTML string #rgb too.

unit ATStringProc_HtmlColor;  interface uses Classes, SysUtils, Graphics; //convert TColor -> HTML color string #rrggbb function SColorToHtmlColor(Color: TColor): string; //convert string which starts with HTML color token #rgb, #rrggbb -> TColor, get len of color-string function SHtmlColorToColor(s: string; out Len: integer; Default: TColor): TColor; implementation function IsCharWord(ch: char): boolean; begin Result:= ch in ['a'..'z', 'A'..'Z', '_', '0'..'9']; end; function IsCharHex(ch: char): boolean; begin Result:= ch in ['0'..'9', 'a'..'f', 'A'..'F']; end; function SColorToHtmlColor(Color: TColor): string; var N: Longint; begin if Color=clNone then begin Result:= ''; exit end; N:= ColorToRGB(Color); Result:= '#'+ IntToHex(Red(N), 2)+ IntToHex(Green(N), 2)+ IntToHex(Blue(N), 2); end; function SHtmlColorToColor(s: string; out Len: integer; Default: TColor): TColor; var N1, N2, N3: integer; i: integer; begin Result:= Default; Len:= 0; if (s<>'') and (s[1]='#') then Delete(s, 1, 1); if (s='') then exit; //delete after first nonword char i:= 1; while (iLength(s)) and IsCharWord(s[i]) do Inc(i); Delete(s, i, Maxint); //allow only #rgb, #rrggbb Len:= Length(s); if (Len<>3) and (Len<>6) then exit; for i:= 1 to Len do if not IsCharHex(s[i]) then exit; if Len=6 then begin N1:= StrToInt('$'+Copy(s, 1, 2)); N2:= StrToInt('$'+Copy(s, 3, 2)); N3:= StrToInt('$'+Copy(s, 5, 2)); end else begin N1:= StrToInt('$'+s[1]+s[1]); N2:= StrToInt('$'+s[2]+s[2]); N3:= StrToInt('$'+s[3]+s[3]); end; Result:= RGBToColor(N1, N2, N3); end; end. 

See also

Источник

How can I convert strings to an html color code hash?

I have a few pieces of text that are using html colors. Color documentation Solution 3: You could easily add the list of HTML colors within your app and translate them.

How can I convert strings to an html color code hash?

I’d like to represent strings as arbitrary html colors.

«blah blah» = #FFCC00
«foo foo 2» = #565656

It doesn’t matter what the actual color code is, so long as it’s a valid hexadecimal HTML color code and the whole spectrum is fairly well represented.

I guess the first step would be to do an MD5 on the string and then somehow convert that to hexadecimal color code ?

Update: Usage example is to generate a visual report of file requests on a server. The colors don’t have to look pretty, it’s more so a human brain can detect patterns, etc in the data more readily.

Thanks for the pointers, this seems to do a competent job:

function stringToColorCode($str) < $code = dechex(crc32($str)); $code = substr($code, 0, 6); return $code; >$str = 'test123'; print ''.$str.''; 

Almost always, just using random colours will

I would recommend creating a (longish) list of colours that work well together and with your background — then just hash the string and modulus (%) with your number of colours to get an index into the table.

public function colorFromString($string) < $colors = [ '#0074D9', '#7FDBFF', '#39CCCC', // this list should be as long as practical to avoid duplicates ]; // generate a partial hash of the string (a full hash is too long for the % operator) $hash = substr(sha1($string), 0, 10); // determine the color index $colorIndex = hexdec($hash) % count($colors); return $colors[$colorIndex]; >

I agree with sje397 above that completely random colours could end up looking nasty. Rather than make a long list of nice-looking colours, I would suggest choosing a constant saturation+luminescence value, and varying the hue based on the content. To get an RGB colour from an HSL colour, you may use something similar to what’s described in http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB .

Here’s an example (try it in http://codepad.viper-7.com something that works, such as https://codepad.remoteinterview.io/ZXBMZWYJFO):

 function hue($tstr) < return unpack('L', hash('adler32', $tstr, true))[1]; >$phrase = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; $words = []; foreach (explode(' ', $phrase) as $word) $words[hue($word)] = $word; ksort($words); foreach ($words as $h => $word) < $col = hsl2rgb($h/0xFFFFFFFF, 0.4, 1); printf('%s ', $col, $word); > ?> 

Convert Hex to RGBA, Since toString on Array joins with , and the fact input can be rrggbbaa hex you can change it to const rgb = hex.match().slice(0,3).map() · 1. below code

How can I convert strings to an html color code hash?

Information credits to stackoverflow, stackexchange network and user contributions. If there any Duration: 1:25

How to use Hex Color Codes in Flutter

Learn how to use the hex color code in a flutter app. We create a function that take hex color Duration: 2:53

Convert a string to a color hex code

Is there a function which converts a string to a color hex code ?

Converting a number with 4 digits into a hex code would be also totally fine, but the problem is that my numbers are very similar.

Does someone know a good solution?

Found a solution on my own with this sha1 hint:

Change random strings into colours, consistently, All you need for a RGB code is a consistent mapping from your random string to a 6 position hex value. Why not use md5 as a way to a hex

HTML Color text to HEX values

I am working on an Android app, which uses some html data from a website. I have a few pieces of text that are using html colors. Like ‘red’ or ‘green’. Is there any way to convert those strings to hex values in java ?

String hexvalue = Integer.toHexString(Color.parseColor(«red»));

This will return a color int

int intColor = android.graphics.Color.parseColor("red") // -65536 

Then you can convert to HEX like so:

String hexColor = String.format("#%06X", (0xFFFFFF & intColor)); 
  • int to HEX conversion: https://stackoverflow.com/a/6540378/363701
  • android.graphics.Color documentation

You could easily add the list of HTML colors within your app and translate them. 140 color names are defined in the HTML and CSS color specification. The list is here.

Given that, it would be trivial to have a HashMap that translates the color names into the appropriate Hex code.

You could also use Color.parseColor as defined here. That would yield an android color -int, which can be converted to hex like this:

String hexColor = String.format("#%06X", (0xFFFFFF & intColor)); 

If they’re using standard CSS ‘red’ and ‘green’ then it’s equivilent to #FF0000 (rgb(255,0,0)) and #00FF00 (rgb(0,255,0)) respectively.

You can also look-up any hex value for a named color in the CSS standard easily at http://www.w3schools.com/cssref/css_colornames.asp

How can I convert string to hex color with javascript, What you can do is loop your list of names, and for each new name store a new colour values in a dictionary. You can then check this dictionary

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Consistently generate the same color for a specific string

License

brandoncorbin/string_to_color

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This project is no longer maintained, check out some of the forks. Or, I recommend string-to-color

Tiny library to generate the same nice hex color for a string in Javascript.

  • Sexy colors, not muddy browns and boring grays
  • Binary safe
  • Only 0.5kb minified

I needed a way to generate a consistent NICE color for any given word to assign a tag it’s own independent color (without knowing what all tags are).

var color = '#'+string_to_color('Some String'); alert(color); 

Optionally, you can pass a percentage to lighten/darken the shade. The default value is -10.

var lighterColor = '#'+string_to_color('Some String', 40); alert(lighterColor); 

About

Consistently generate the same color for a specific string

Источник

Color Translator. From Html(String) Method

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Translates an HTML color representation to a GDI+ Color structure.

public: static System::Drawing::Color FromHtml(System::String ^ htmlColor);
public static System.Drawing.Color FromHtml (string htmlColor);
static member FromHtml : string -> System.Drawing.Color
Public Shared Function FromHtml (htmlColor As String) As Color

Parameters

The string representation of the Html color to translate.

Returns

The Color structure that represents the translated HTML color or Empty if htmlColor is null .

Exceptions

htmlColor is not a valid HTML color name.

Examples

The following example is designed for use with Windows Forms, and it requires PaintEventArgs e , which is a parameter of the Paint event handler. The code translates an HTML color name to a Color structure, and then uses that color to fill a rectangle.

public: void FromHtml_Example( PaintEventArgs^ e ) < // Create a string representation of an HTML color. String^ htmlColor = "Blue"; // Translate htmlColor to a GDI+ Color structure. Color myColor = ColorTranslator::FromHtml( htmlColor ); // Fill a rectangle with myColor. e->Graphics->FillRectangle( gcnew SolidBrush( myColor ), 0, 0, 100, 100 ); > 
public void FromHtml_Example(PaintEventArgs e) < // Create a string representation of an HTML color. string htmlColor = "Blue"; // Translate htmlColor to a GDI+ Color structure. Color myColor = ColorTranslator.FromHtml(htmlColor); // Fill a rectangle with myColor. e.Graphics.FillRectangle( new SolidBrush(myColor), 0, 0, 100, 100); >
Public Sub FromHtml_Example(ByVal e As PaintEventArgs) ' Create a string representation of an HTML color. Dim htmlColor As String = "Blue" ' Translate htmlColor to a GDI+ Color structure. Dim myColor As Color = ColorTranslator.FromHtml(htmlColor) ' Fill a rectangle with myColor. e.Graphics.FillRectangle(New SolidBrush(myColor), 0, 0, 100, 100) End Sub 

Remarks

This method translates a string representation of an HTML color name, such as Blue or Red, to a GDI+ Color structure.

Источник

Читайте также:  Php empty but 0
Оцените статью