Invert colors in UIWebView or UITextView - iphone

I was wondering if anyone knew if there was an easy way to invert the colors of a UIWebView or UITextView. I know it is possible to invert all on your iPhone, but I do not want the user to have to do that, they should be able to with one click in app, invert the colors so that if it is a low light situation, reading something will be easier and less intrusive to people around the user.
Thanks for any help!

Here is one way I've inverted colors in a UIWebView (in the past) by injecting javascript.
First...
#define invertJS #"function load_script(src,callback){var s=document.createElement('script');s.src=src;s.onload=callback;document.getElementsByTagName('head')[0].appendChild(s);}function invertColors(){var colorProperties=['color','background-color'];$('*').each(function(){var color=null;for(var prop in colorProperties){prop=colorProperties[prop];if(!$(this).css(prop))continue;color=new RGBColor($(this).css(prop));if(color.ok){$(this).css(prop,'rgb('+(255-color.r)+','+(255-color.g)+','+(255-color.b)+')');}color=null;}});}load_script('http://www.phpied.com/files/rgbcolor/rgbcolor.js',function(){if(!window.jQuery)load_script('https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',invertColors);else invertColors();})"
And then when your webview finishes loading
- (void)webViewDidFinishLoad:(UIWebView *)webview{
[webview stringByEvaluatingJavaScriptFromString:invertJS];}

For a UITextView you can set the text color and background color attributes, I know from there you could set up a button to switch between white back/black text and black back/white text. Unfortunately, I do not see any similar attributes for UIWebView.
See iOS Documentation for UITextViews and UIWebViews.
Also when passing colors to these attributes, you can use Hex values instead of Apple's preset colors. In this way you could call the hex value of the attribute, invert it with any number of formulas, and pass the new hex value back to the attribute.
Inverting a Hex color, put simply:
// maximum hex value is FFFFFF, so
newHex = FFFFFF - oldHex;

To get the ARGB components of any UIColor, you can use the code provided in this answer:
Extracting rgb from UIColor
You can then "invert" the colors (sort of) by subtracting the r, g and b components (but not the a) from 1.0, and then using these modified components to produce a new color, like so:
r = 1.0 - r;
g = 1.0 - g;
b = 1.0 - b;
UIColor newColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0];

I ended up just making two separate files on my server. One that is inverted and one that is regular. There does not seem to be anything in core graphics at least that I could find that would easily do this and to be honest HTML or CSS are definitely not my strong points. Still if anyone has a simpler solution for this I am more than interested in a good idea.

Related

Fade out end of text label strings that don't fit (instead of truncate)

I'm not sure if "fade out" is the correct term to use, as it seems to refer to the animation effect a lot, but what I'd like to achieve can be seen in the address bar of the iPhone Safari app. When a URL is too long to display, the end of the string "faded out" rather than truncated with "...".
I thought this could be easily done by changing the Line Breaks setting from "Truncate Tail" to "Clip" in the XIB file and then using an image with transparency to create the effect. But setting to "Clip" seems to clip at the end of a word, rather than the middle of a word, or the middle of a letter even, as seen in Safari or Chrome for iPhone. This doesn't quite work for me, and it actually gives the impression that the text is complete, when upon closer inspection, the user will notice that the text doesn't make sense.
What's the best way to "fade out" strings that don't fit in text labels? Thanks in advance.
Take a look at the Google Toolbox for Mac GTMFadeTruncatingLabel, which is a reusable component that does exactly this.
You could also use a combination of the category found here : (https://stackoverflow.com/a/24508153/2654425) and a gradient.
And do:
if([myLabel isTruncated]){
CAGradientLayer *l = [CAGradientLayer layer];
l.frame = myLabel.bounds;
l.colors = #[(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor];
l.startPoint = CGPointMake(1.f, .1f);
l.endPoint = CGPointMake(0.95f, 1.0f);
myLabel.layer.mask = l;
}
This works in iOS 7 & iOS8.

How to get RGB components of a custom color in ios

I would like to set custom color programmatically using the method
[UIColor colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha];
For which i need to know the value of RBG components.
I would like to know is there any way by which i can get the RBG components of a custom color so that i can use them in the above mentioned method.
You can use the DigitalColor Meter.app included with every Mac OS X install. You can find it in ~/Applications/Utilities/DigitalColor Meter.app. Use it to inspect the RGB values of any pixel you mouse over. Once you have the values, you just need to divide them by 255.0 because +colorWithRed:green:blue:alpha: is expecting a floating point value between 0 and 1.
[UIColor colorWithRed:83.0f/255.0f green:217.0f/255.0f blue:58.0f/255.0f alpha:1.0f];
You can use Xcode itself to get RGB value of custom color:
Click the arrow for the Color box and in the resulting dialog box choose others.
And then you can use RGB Slider option to get RGB values
To match a color exactly, there is a great utility called "DigitalColor Meter" located in Applications/Utilities/ that can get the RGB for anything on your screen. Take a screen shot of the color you want to replicate, then use this tool to get the information.
Actually there is no need for using DigitalColor Meter instead we can use the small Search Icon on color picker in the Interface Builder with which we can exactly adjust the color that we like.It is also possible to look up the color values using RBG scales available in it.

How get a random UIColor but restrict it to subset of the colors

I know how get a random UIColor, but I wonder if exist a way to only return colors with some specific quality of it. I don't know if exist a specific term, but for example, I need to get only opaque colors, dark tones... colors that are good for backgrounds with bright text/ images on it.
I could hand-pick some, but in my case i want to provide a very good defaults for a large list of icons backgrounds.
UPDATE:
My solution based on the answer here:
float r = arc4random() % 12;
return [UIColor colorWithHue:(30*r)/360 saturation:0.5f brightness:0.8f alpha:1.0f];
I get a very small list of possible answers, but this give me a good start.
What I would do is use the HSV/HSL colour space and then convert the results to RGB. Keep a constant value and saturation (S and V) whose qualities you like, and then alter the hue (H) value for the number of colours you want. Convert the results to RGB and you'll have a nice set of colours that appear to work well together. You can create any set of nice colours that are 'saturated', 'bright', 'dark', etc. just by picking nice combinations of S and V for your needs (low V is dark, high V is bright, low S is unsaturated, high S is saturated).
Take a look at this wiki page for a discussion on converting HSV to RGB. I'd recommend you start with saturation (S) at 0.5 and value (V) at 0.8. Then, to generate nice colours, do the HSV -> RGB conversion for H values of 0, 30, 60, 90... all the way up to 360. It will generate a nice palette of colours that look good together. Later, you can adjust your S and V values if you want different colour 'qualities'.
Using a random color as a background is indeed a suboptimal solution as it will lead to visibility and readability problems of the text and information of your icon. There is some research going on in automatically finding an appropriate color that matches your foreground colors, leading to a harmonic result in terms of visual perception of the image. Have a look at http://cs.nyu.edu/~sorkine/ProjectPages/Harmonization/

iPad UIColor Saturation Issues

I am trying to draw a UIColor on the screen of a view-based app, and I am trying to do so using HSB. It is absolutely necessary for me to use HSB in this case. I can create a UIColor object with any S value from 0.0f to 0.75f, but past that the numerical changes have no effect on the actual saturation displayed. I need it to be 1.0f, but it is still using 0.75f. Any ideas on why it is doing that, and how I can make it work?
Because of how it works, + (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha actually does not use HSBA values internally; it is simply a wrapper around the device RGB color space.
I think that under extreme cases there surely would be chances that a constant H/B/A + a .75–1 S yields colors that differ so slightly it became imperceptible, despite the color components being digitally tracked as very precise floats. As saturation drops, the number of “available” colors decreases (as the display could only show this many colors, dropping the saturation compresses the usable colors) and the chance of collision simply rises.
Given that your scenario uses H0-1, B1, A1 colors which nearly invalidates my assumption, I was curious and have made a test project; the colors however worked correctly. I’m on iOS 4 SDK GM, so maybe it’ll help if we know which SDK you’re working against.
After doing some experimentation, I've discovered what my issue was.
I was using a for loop to draw single-pixel lines across a view, each with a hue value greater than the previous one. I was doing this to create a color spectrum to be used for a color picker.
My issue arose because I was using CGContext paths, not rects, to do the drawing. Paths, by default, "straddle" the created path with pixels. Because I was setting the width to one, CoreGraphics was forced to average between pixels, creating a desaturated effect. Setting the width to two set the saturation correctly, but the gradient of the spectrum was no longer smooth.
My fix for this issue was to use rects instead of paths. They did not blend between pixels, and the saturation issue was fixed.

UILabel Question. Aligning overlapping UILabels

I have two UILabels. that I want to overlap one atop the other. Call the labels "under" and "over".
over: A C E G
under: B D F
UILabel "over" will have its text drawn in red. "under" will be in blue. The visual effect will be alternating colors between successive letters.
What are the controls available to me to exactly align the text in each label to pull this off?
Cheers,
Doug
I agree with fbrereton. This seems a very hard way to achieve the goal. Check out the NSString UIKit Additions to learn how to draw your own strings and lay the characters out yourself in your own custom -drawRect:. You'll have far greater control, and the code should not be that complex. iPhone doesn't have very good layout support (nothing like Mac offers), but for something this simple, it shouldn't be too bad.
You have to make sure the font you are using is a monospace font, otherwise the characters will not line up exactly as you would hope with just a space between them. (I believe there is a typewrite font available in the iPhone OS that is monospace; YMMV.) Also you will have to prefix underLabel with a space for the code below to work.
To map one UILabel on top of another, try:
overLabel.opaque = NO; // so you can see what is under overLabel
overLabel.textColor = [UIColor redColor];
overLabel.backgroundColor = [UIColor clearColor];
underLabel.frame = overLabel.frame;
underLabel.textColor = [UIColor blueColor];
Note in the above code underLabel takes on overLabel's frame because the latter's frame is wider; if it were the other way around overLabel would get clipped.
All that being said I'd wager there is a better way to skin this particular cat. This solution feels very "round peg, square hole" to me.