Adjacent colors in swift - swift

I'm using an API which returns images based on their color. I send a hex value, i.e. FF0000 and it returns images with that red color.
However, the API is rather too specific. For the above color for instance, I'd want to return all red stuff, not just that specific red.
Luckily the API allows a union of colors FF0000u0000FF is pure red and blue.
So what I need to do create an array of colors which surround the main color, so that I can pass it to the API,
How would I do this in Swift?

There are well over 16 million colours available in that colour space so I don't think that creating an array will be practical as there could easily be hundreds of thousands of 'reds' to add to that array. A better approach would be to adapt the api to compare a range of values of each separate channel so rather than red equating to FF0000, you would say that 'red' is any colour where the red channel is greater than 88 (HEX) and the other channels are less than 66 (HEX) for instance. It's a complex task that you're trying to achieve so it won't be a trivial job to solve it. If you share some code or a link to the API, someone may be able to give you more specific help.

Related

Change Shape Fill based on Shape Data in Visio

Wondering if someone can help me with what I thought was a simple Shapesheet issue.
I'd like the fill colour of a shape to change based on one of the shape data values. I thought I could do this easily:
FillForeground: If(Prop.Colour="Blue",XXX,YYY)
But this doesn't work? (Visio 2021)
I've 5 list values for the data and would like a different fill colour for each? Weirdly, I can see that the value does change based on the data selection, but the actual colour seen in on the page doesn't.
Obviously, I'm not much of a coder, just an enthusiastic meddler.
I've stuck at:
=IF(Sheet.3780!Prop.StringSection="Firsts",THEMEGUARD(MSOTINT(THEME("LineColor"),40)),THEMEGUARD(MSOTINT(THEME("AccentColor"),40)))
(The shapes to colour are within a group)
Thanks
Steve
Steve !
FillForeground: If(Prop.Colour="Blue",XXX,YYY)
But this doesn't work? (Visio 2021)
For compare strings in ShapeSheet you must use STRSAME function
Use syntax like this:
IF(STRSAME(Prop.color,"blue"),4,3)

Is it possible to tell the human readable color (ex. pink, white) from RGB/Hex code in Swift? [duplicate]

This question already has answers here:
Check if color is blue(ish), red(ish), green(ish),
(3 answers)
Closed 1 year ago.
I'm trying to build an app for a class project that finds the most dominant color of an image. While it's not hard to extract the most dominant colors RBG code, I was wondering if there is a way to use this code to get us the name of the color like red color or blue color.
I understand this would be technically complex since there are so many different RGB values but I was wondering if that had been done before. I'm using Swift to develop this app.
This question is not swift related but more a general programming problem. There are multiple ways to solve this.
The first approach would be to create a list of colors you want to separate, or get it from somewhere. Then create a function that maps a random RGB value onto the nearest values from the list (you can do least squares or any kind of definition of 'nearest values').
Another solution would be to again use a mapping but based on the angle of the rgb values when mapped into an rgb color wheel (https://www.pikpng.com/pngl/b/113-1130205_alt-text-rgb-led-color-mixing-chart-clipart.png, http://www.procato.com/rgb+index/).
Anyway, there are multiple solutions online and also on stack overflow (RGB color space to raw color name mapping, https://github.com/ayushoriginal/Optimized-RGB-To-ColorName)

Altair Scatter Chart

i am looking for a way to make a point chart based on multiple nominal conditions. i am plotting 2 values on x and y, but i would like to differentiate these points based on year as well as 'type'.
currently, the way i do it is to assign year to color while 'type' is assigned to shape
color=alt.condition(selection, alt.Color('Date:T'), alt.value('lightgray'), scheme='red' )
shape = alt.Shape('type:N')
a few questions:
is it possible to change the color scheme of the points instead of the default colors to say shades of red/blue/black, etc?
is it possible to assign one color scheme/shade (instead of shapes) to 'types'?
is it possible to change the color scheme of the points instead of the default colors to say shades of red/blue/black, etc?
Yes, see https://altair-viz.github.io/user_guide/customization.html#customizing-colors. You can use any of the built-in Vega color schemes, or define your own using the methods discussed there. From your example, it might look something like this:
color=alt.condition(
selection,
alt.Color('Date:T', scale=alt.Scale(scheme='reds')),
alt.value('lightgray')
)
is it possible to assign one color scheme/shade (instead of shapes) to 'types'?
No, there is no built-in way to apply two color scales based on two fields in the data (how would a mark choose between the two colors assigned to it?) One possible approach would be to use an opacity encoding for the second field, which is reflected in the lightness of the marks. For your example, it might look like this:
opacity='type:N'

How to change the histogram colour in Matlab

So i'm awful at Matlab and I mainly learn through examples and literally spelt out explanations.
So baring that in mind - Right now i'm trying to find how likely it is that one image i'm given is in anther via a histogram.
What i want to do is create 3 histograms for red, blue & green for each image and then add those into one image - So basically i'll have an image with a literal green histogram showing the green, a red one showing the red and a blue one showing the blue.
I know that to show a colour chanel in matlab i have to do imhist(image(:,:,1/2/3)
however that still gives me a histogram in blue.
I've looked up some things that are meant to help with this issue but it's normally aimed toward someone who knows what they're doing.... not helpful.
I've heard peope saying something like get(get(gca,'child')) which just seems giberish to me.
SO - for what i'm trying to do, image detection via histrograms, is this an appropriate method? And if so HOW do i create my 1 histogram that shows all 3 histograms in their respective colour
Cheers
You could use this version of imhist:
[counts,x] = imhist(...)
And then draw your histograms yourself, via bar, stem or similar.
These functions are then fully customizable and you can plug in your favourite color, linestyle, etc.

Pixel color matching estimate

For image scanning purposes, I'd like a pixel (which I can get from a UIImage) to match (for a certain percentage) to a pre-set color.
Say pink. When I scan the image for pixels that are pink, I want a function to return a percentage of how much the RGB value in the pixel looks like my pre-set RGB value. This way I'd like all (well, most) pink pixels to become 'visible' to me, not just exact matches.
Is anyone familiar with such an approach? How would you do something like this?
Thanks in advance.
UPDATE: thank you all for your answers so far. I accepted the answer from Damien Pollet because it helped me further and I came to the conclusion that calculating the vector difference between two RGB colors does it perfectly for me (at this moment). It might need some tweaking over time but for now I use the following (in objective c):
float difference = pow( pow((red1 - red2), 2) + pow((green1 - green2), 2) + pow((blue1 - blue2), 2), 0.5 );
If this difference is below 85, I accept the color as my target color. Since my algorithm needs no precision, I'm ok with this solution :)
UPDATE 2: on my search for more I found the following URL which might be quite (understatement) useful for you if you are looking for something similar.
http://www.sunsetlakesoftware.com/2010/10/22/gpu-accelerated-video-processing-mac-and-ios
I would say just compute the vector difference to your target color, and check that it's norm is less than some threshold. I suspect some color spaces are better than others at this, maybe HSL or L*ab, since they separate the brightness from the color hue itself, and so might represent a small perceptual difference by a smaller color vector...
Also, see this related question
Scientific answer: You should convert both colors to the LAB color space and calculate the euclidian distance there. That value is also called deltaE.
The LAB space was developed (using test persons) for exactly that reaason: so that different color pairs with equal distances in tnis space correspond to equal perceived color differences.
However, it sounds like you are not looking for matching a specific color, but rather a color range (lets say all skin tones). That might require more user input than just a reference color + a deltaE tollerance:
a reference color with 3 tollerances for hue, saturation and brightness
a cloud of refence color samples
...