Flutter eye dropper - flutter

I am trying to find eye dropper for flutter or implement one. Can anybody help me with that?
I want to tap an image and get the colour of tapped area. Similar to an eyedropper for android.link
final View targetView = findViewById(R.id.targerView);
// Any view from which you want to pick the color
new EyeDropper(targetView, new ColorSelectionListener() {
#Override
public void onColorSelected(#ColorInt int color) {
// color is the color selected when you touch the targetView
(findViewById(R.id.colorSlot)).setBackgroundColor(color);
}
});
I just need something that allows me to pick a colour from an image just by tapping an area of an image and get the colour of that area.

I found a plugin for it. https://pub.dev/packages/cyclop it has the option you required.

Related

How to remove displayImage tint in Edit Widget view?

I have an IntentHandler where I am setting the displayImage value for the configuration options being provided to my app widget.
On the 'Edit Widget' screen (accessed by long-pressing the Widget), a tint is being applied that is rendering the image entirely blue:
If I tap the value to see all available options, the images are rendered normally.
I suspect the tint is due to the image being part of a button, but as far as I'm aware I don't have direct access to the button to change its options.
Here is a simplified version of the IntentHandler class:
class IntentHandler: INExtension, ConfigurationIntentHandling {
func provideMyDataOptionsCollection(for intent: ConfigurationIntent, with completion: #escaping (INObjectCollection<MyData>?, Error?) -> Void) {
var dataForWidget = [MyData]()
// Retrieve dynamic data here...
let myData = MyData(identifier: String(id), display: name)
// Retrieve corresponding image here...
myData.displayImage = INImage(imageData: (retrievedImage.pngData())!)
dataForWidget.append(myData)
let collection = INObjectCollection(items: dataForWidget)
completion(collection, nil)
}
What is the best way around this?
I was having the exact same issue and came across this post - How do I turn off the blue overlay on the image I'm using with my UIButton?
I ended up following the 2nd approach and going to my Asset Catalog and changing the image's default rendering type to "Original" for the affected images.
This removed the blue tint on the widget customization screen when a selection is made.

SwiftUI Color doesn't update on main page

All my text in my app use the color Color.textColor which changes due to a UserDefault setting:
extension Color {
public static var textColor: Color {
let color = UserDefaults.standard.bool(forKey: "redText") ? Color.red : Color.white
return color
}
}
This is changed via a toggle in my settings section. The problem is, when I toggle this it successfully changes all text colours all my views, apart from the main view.
I've made a gif here to showcase the problem: https://imgur.com/a/LcN7TBi
The bug is there in both simulation and preview.
Edit: When I switch off/on the preview the main page colours actually update to the correct colour.

How to get rid of the white shadow in dark mode in macOS?

I am making a macOS app in Swift and am seeing a weird issue when I switch b/w light and dark modes. The shadow that appears around the image on the light mode is great but the one I am getting by default on the dark mode doesn't look nice. Does anyone know how to fix this? I am getting the same for all other UI elements like Checkboxes, Radio Buttons, etc.
Not nice
Nice
Note:
This image is added via the Interface Builder in Xcode.
I have the shadow checkbox unchecked as well. See below:
Make sure that the superviews of your view don't define a shadow either or that it has an opaque background.
If your superview doesn't have an opaque background, child views will also have a shadow.
You can either try implementing viewDidChangeEffectiveAppearance() on your NSView or do key-value observations on NSApp.effectiveAppearance. This way you'll be notified about appearance changes between light/dark mode so you can react accordingly.
You can detect whether a view is Dark Mode or not. And you hide shadow when in dark mode.
Add an extension to NSView:
extension NSView {
func isDarkMode() -> Bool {
if #available(OSX 10.14, *) {
return effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
}
return false
}
}
Create a custom view (view, button etc). Override viewDidChangeEffectiveAppearance method in your custom view and update layer.
class CustomView: NSView {
override func viewDidChangeEffectiveAppearance() {
// edit shadow's properties. ex: shadowOpacity.
if isDarkMode() {
// dark mode
} else {
// light mode
}
}
}

Xcode: Setting UIControl background colour resets on orientation change

In short, I made 'radio button' like buttons. When you press on button, its background gets set to dark green, and the others are set to light green.
However, when I rotate the device (I'm using the emulator) then the colour resets to light green - it's original colour as set up in the .xib file.
Each of the green 'buttons' is a UIControl. I set the colour using
button.backgroundColor = unselectedColour
So, like Alexander suggested, I managed to get this working using a property observer.
Like mentioned above, I have 3 buttons. I also have an enum to help remember which button has been selected (eg. none, button1, button2, button3). I keep a variable to keep track of this enum, with the property observer like so:
/** Remember the last trailer selection made. */
var lastSelection: MyEnum = .none {
willSet {
setButtonColours(selection: newValue)
}
didSet {
}
}
Each button's action sets the lastSelection variable, which then triggers this. Finally, I had to add this little bit of code to make sure that the redraw happens after the orientation change:
override func viewDidLayoutSubviews() {
setButtonColours(selection: lastSelection)
}
I don't know if this is the BEST solution (as I'm a noob), but it works! Thank you again Alexander for the help!

Changing Label/Button Text colour dynamically based on the dynamically changing background

I know it may sound a little bit of a drag to ask this question but I am curious if anything like this is available.
I am building a app in which the background image (covers full view with a blur effect) of each view controller changes dynamically. This background image will be in all the view controllers, each one having a different set of UIControls (Labels, Buttons, Table Views, Collection Views, containers, tabs, etc).
Sometimes when the background image is very light, the foreground texts (labels, buttons) with white text colour are not visible at all. Also the vice versa is a problem too.
So I would like to know if there is any way to change the foreground text colour dynamically based on its background.
Recently I faced the same problem, And I think what you looking for is either image is bright or dark so that you can set property accordingly Hope this will help.
Create observer, everytime image change it will check if its a dark image or bright and based on that will call the function UIForDarkImage and UIForBrightImage
//ImageView observer to observe the image change and perform the UI action based on the image colour
//Your imageView you are using to set image
var imageView: UIImageView {
didSet {
if imageView.isDark(image.bounds) { //dont pass the full image bounds pass the rect where your buttons or label places it will save your hell lot of time
setUIForDarkImage() // here you set buttons and labels color to white or whatever changes you want to perform based dark image
}
else {
setUIForBrightImage() // here you set buttons and labels color to black or whatever changes you want to perform based bright image
}
}
}
UIImageViewExtension for checking if Image is a dark image or bright Image.
What happening is, It will go through image pixel-by-pixel and check if the pixel is bright or dark and if we get dark pixels more than the threshold we have set, we will assuming that its a dark image else it's a bright image.
PS: For better efficiency not checking the whole image (for a high-resolution image it will slow down if we will check all pixels) so only checking the part of the image in which we need our button or label, you can set rect based on your requirement. And also scaling by 0.45 to check few pixels in that rect(you can increase or decrease for more/less accuracy).
extension UIImageView {
func isDark(_ rect:CGRect)->Bool {
let s=image?.cgImage?.cropping(to: rect);
let data=s?.dataProvider?.data;
if data == nil {return false;}
guard let ptr = CFDataGetBytePtr(data) else {return false;}
let threshold = Int(Double(rect.width * rect.height) * 0.45);
var dark = 0,len=CFDataGetLength(data);
for i in stride(from: 0, to: len, by: 4) {
let r = ptr[i], g = ptr[i+1], b = ptr[i+2];
if (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b)) < 100 {dark += 1;}
if dark > threshold {return true;}
}
return false;
}
}
PS: If you also like to know what I am doing in setUIForBrightImage() and setUIForDarkImage do let me know
Here's what I would do: Create a struct that contains the name of the image view and a tint color to use for your labels, buttons, etc.
Add a property to your view controllers using this new struct. Let's call it backgroundImageSettings.
Add a didSet method to the property that calls another method useBackgroundImageSettings() that installs the image into the background of the view controller's content view, sets the tint color for the view controller's content view, and calls setNeedsDisplay() on the content view.. Also call useBackgroundImageSettings() in your viewDidLoad so that setting your backgroundImageSettings still works even if you view controller's view hasn't been loaded yet.
I like Duncan's answer! An additional step you can take is to set a slight overlay onto your background image.
For example, if you have a background image with really dark colors and you can't see your black text very well, then you can add a layer with a background color of white and an opacity less than 1. That way there's a bit of white to make your text more readable, and you can still see the background image.
let overlay = UIView(frame: yourSuperView.bounds)
overlay.backgroundColor = UIColor.white
overlay.layer.opacity = 0.5
yourSuperView.addSubview(overlay)