How to Convert a string to Hex? - asp.net

Is there any way to convert a string to hexa and then to convert it to a .Net color?
I would like to know how to convert a string of color ,say Black, to its Hexa '#000000' ?
i.e. if my input is "Black", i should return "#000000"
My issue is:
i'm setting color and storing its name in an object. So, if it is white, the object keeps "white" ,but for certain shades, it is keeping the name as f12a12 ( an example). I appended "0x" befor such strings and it worked fine with the colortranslator. In case of the normal colors in Color object, i dont want to append this. I can make the string to search through the Colors but i would like to know whether there is any other way to do this?

Color c = Color.Black;
string strColor = System.Drawing.ColorTranslator.ToHtml(c);
//returns 000000
Edit:
In reverse
Color c = System.Drawing.ColorTranslator.FromHtml("#000000");

There is no way to get the HEX from the color name. You need to create a lookup table which holds the name of the color and also the HEX of that color. And only then you can get the HEX of that color.
For your solution I am not sure, but I think to get the correct RGB values you need to have HEX of that color.

ColorTranslator.FromHtml( "#ffffff")

Related

wordcloud2 does not display numbered colors

When I use numbered colors in wordcloud2, nothing is displayed.
library(wordcloud2)
wordcloud2(demoFreq,color='blue1')
Color names without numbers are ok. E.g. color='blue'
What am I missing?
Since the word cloud is being rendered in HTML, you need to use html colors. blue1 is an R color, not HTML, so you need to convert it to a hex value color. the R function col2rgb will give an RGB triple, but that will not work with HTML. You need to convert the triple to a hex value. You can do that using rgb. However, rgb expects 3 columns, not a column of 3 values, so use t to transpose the RGB values.
rgb(t(col2rgb("blue1")), maxColorValue = 255)
[1] "#0000FF"
Now you can successfully call wordcloud2
wordcloud2(demoFreq, color=rgb(t(col2rgb("blue1")), maxColorValue = 255))
Probably HTML color codes are meant by "numbered colors", e.g.
wordcloud2(demoFreq, color= "#0080CC")
When "blue1" is not defined anywhere wordcloud2() has no color to use. But you could define one yourself.
library(wordcloud2)
blue1 <- "#0080CC"
wordcloud2(demoFreq, color= blue1)
Note that there are no quotes "" in this case.
There are more color names available here. The package itself defines 'random-dark' and 'random-light' in addition.
Edit: According to #GW5's comment use following code to obtain exactly the desired "blue1"as a workaround:
blue1 <- colors()[27]
wordcloud2(demoFreq, color= blue1)

How to get the name of a UIColor?

I have an array of UIColors like
[UIColor blueColor];
is it possible to get the name "blue" from that UIColor, to display it in a string?
There's nothing built in to UIKit, but Erica Sadun's uicolor-utilities library adds similar functionality.
You can get RGB and Alpha of the UIColor, if you want.
A list of color names can be obtained using runtime.
See my question: How to get all Color methods of UIColor?
You can also use string methods to convert 'xxxColor' to 'xxx' if needed.

changing color scheme vim

I'm using VIM in red hat 5, with the black background and green letters. However, I don't like the color schema for strings ("") nor reserved C++ key variables (int for example it is in dark green). Could someone please give me some suggestions how could I change those default colors?
I know, for example, to change the comment collor I do
hl Comment ctermfg=yellow
Type :hi, then hit Enter. A list of highlighting definition will show.
The C++ groups start with c.
int is a cType. Try:
:hi cType ctermfg=yellow
You can put your custom highlighting commands into vimrc(after colorscheme name and syntax on).
If you'd want to customize your current colorscheme, check out this Vivify. Adjust the color editor to your specific language and then save the colorscheme file into your vimfiles.

Can I turn off SASS RGB -> Color Name

I have a CSS rule like this:
background: #fff url('/assets/img/file.png');
It is compiling to:
background: white url("/assets/img/file.png");
Is there any way to prevent it from converting it like that? There is JS on my page that looks for RGB values and I don't want to have to convert those strings to RGB in some hacky function.
By default, Sass will not convert literal color values from their hex values unless you are forcing Sass to interpolate with #{} or a variable.
Using interpolation will return the "to_sass" version of the value you're interested in. For example, #{ #fff } will interpolate to "white". This also happens during variable replacements: color literals are translated to Color objects when used as variables, then "to_sass"ed into your stylesheet.
Furthermore, you may specify the style option compressed, which will return the less byte-length version (i.e. red instead of #f00). Since white is 5 characters long and #fff is only 4, your rule will replace with #fff instead.
There is no way to turn off the reverse HTML4 color name conversion when using variables. As a work-around, you can declare color variables as a string, then use then in styles with the unquote() function.
$color: '#fff';
.white { color: unquote($color) }

How to Convert Qcolor value to hex value?

I have implemented a QColor dialog box which opens on certain condition. To get the selected color after final selection I use the method selectedColor() which returns the value in QColor. When I print that value, it's like this:
<PyQt4.QtGui.QColor object at 0x01DD7880>
I want color value in hex value like this: #DFDFDF (for grey). If it's not hex, correct me.
Is there any function to convert that?
Any suggestions welcome.
You need to print selectedColor().name() to print the actual color value in hex. See the QColor Documentation
To amplify a bit, maybe confuse, maybe clarify... (For Python newbies)
color = QColorDialog.getColor(pWidget.textBackgroundColor(), pWidget, 'Get Text Highlighting Color')
The above will return a QColor using the QColorDialog, for those of us who don't want to be stuck with named colors like 'Blue', 'red', green etc.
fg = color.name()
In this case I am converting the QColor to a string HEX for use in a style sheet.
Widget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)
This is how such a converted value can be used in a style sheet.
Note how to concatenate more than one stylesheet attribute. Also, side note, sometimes changing one attribute cancels previous changes to others.

Resources