iran-react-community/rn-dominant-color

get color of locale image

Psiiirus opened this issue · 13 comments

Hiya,
is it possible to extent this lib to also extract colors of locale images?

Cheers,
Toni

I'm a total noob in swift so it took me a while to get a version working on iOS with asset-library:// URL
Maybe this code can help to make a fast implemention?
Not doing a PR because the lack of Android support

//
//  DominantColor.swift
//  RNDominant Color
//
//  Created by Matin ZD on 7/11/1398 AP.
//  Copyright © 1398 Facebook. All rights reserved.
//

import Foundation
import UIKit
import Photos // use the Photos Framework

@objc(RNDominantColor)
class RNDominantColor : NSObject {
  
    @objc static func requiresMainQueueSetup() -> Bool {
        return false
    }
    
    @objc func getColorFromURI(_ imageURI: String,callback successCallback: @escaping RCTResponseSenderBlock ) {
        let url = URL(string: imageURI as String)
        
        let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [url!], options: nil)
        if let phAsset = fetchResult.firstObject {
            PHImageManager.default().requestImageData(for: phAsset, options: nil) {
                (imageData, dataURI, orientation, info) -> Void in
                if let imageDataExists = imageData {
                    guard let colors = UIImage(data: imageDataExists)?.getColors() else {
                      return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
                    }
                    successCallback([
                      colors.primaryHex,
                      colors.secondaryHex,
                      colors.backgroundHex,
                      colors.detailHex
                    ])
                }
            }
        } else {
          return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
        }
    }

    @objc func getColorFromURL(_ imageURL: NSString,callback successCallback: RCTResponseSenderBlock ) {
        let url = URL(string: imageURL as String)
        let data = try? Data(contentsOf: url!)
        guard let colors = UIImage(data: data!)?.getColors() else {
          return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
        }
        successCallback([
          colors.primaryHex,
          colors.secondaryHex,
          colors.backgroundHex,
          colors.detailHex
        ])
    }
}

Hiya,
is it possible to extent this lib to also extract colors of locale images?

Cheers,
Toni

Work in progress

I'm a total noob in swift so it took me a while to get a version working on iOS with asset-library:// URL
Maybe this code can help to make a fast implemention?
Not doing a PR because the lack of Android support

//
//  DominantColor.swift
//  RNDominant Color
//
//  Created by Matin ZD on 7/11/1398 AP.
//  Copyright © 1398 Facebook. All rights reserved.
//

import Foundation
import UIKit
import Photos // use the Photos Framework

@objc(RNDominantColor)
class RNDominantColor : NSObject {
  
    @objc static func requiresMainQueueSetup() -> Bool {
        return false
    }
    
    @objc func getColorFromURI(_ imageURI: String,callback successCallback: @escaping RCTResponseSenderBlock ) {
        let url = URL(string: imageURI as String)
        
        let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [url!], options: nil)
        if let phAsset = fetchResult.firstObject {
            PHImageManager.default().requestImageData(for: phAsset, options: nil) {
                (imageData, dataURI, orientation, info) -> Void in
                if let imageDataExists = imageData {
                    guard let colors = UIImage(data: imageDataExists)?.getColors() else {
                      return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
                    }
                    successCallback([
                      colors.primaryHex,
                      colors.secondaryHex,
                      colors.backgroundHex,
                      colors.detailHex
                    ])
                }
            }
        } else {
          return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
        }
    }

    @objc func getColorFromURL(_ imageURL: NSString,callback successCallback: RCTResponseSenderBlock ) {
        let url = URL(string: imageURL as String)
        let data = try? Data(contentsOf: url!)
        guard let colors = UIImage(data: data!)?.getColors() else {
          return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
        }
        successCallback([
          colors.primaryHex,
          colors.secondaryHex,
          colors.backgroundHex,
          colors.detailHex
        ])
    }
}

Make Pull request please and we will workaround android :) Thanks!

I'm a total noob in swift so it took me a while to get a version working on iOS with asset-library:// URL
Maybe this code can help to make a fast implemention?
Not doing a PR because the lack of Android support

//
//  DominantColor.swift
//  RNDominant Color
//
//  Created by Matin ZD on 7/11/1398 AP.
//  Copyright © 1398 Facebook. All rights reserved.
//

import Foundation
import UIKit
import Photos // use the Photos Framework

@objc(RNDominantColor)
class RNDominantColor : NSObject {
  
    @objc static func requiresMainQueueSetup() -> Bool {
        return false
    }
    
    @objc func getColorFromURI(_ imageURI: String,callback successCallback: @escaping RCTResponseSenderBlock ) {
        let url = URL(string: imageURI as String)
        
        let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [url!], options: nil)
        if let phAsset = fetchResult.firstObject {
            PHImageManager.default().requestImageData(for: phAsset, options: nil) {
                (imageData, dataURI, orientation, info) -> Void in
                if let imageDataExists = imageData {
                    guard let colors = UIImage(data: imageDataExists)?.getColors() else {
                      return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
                    }
                    successCallback([
                      colors.primaryHex,
                      colors.secondaryHex,
                      colors.backgroundHex,
                      colors.detailHex
                    ])
                }
            }
        } else {
          return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
        }
    }

    @objc func getColorFromURL(_ imageURL: NSString,callback successCallback: RCTResponseSenderBlock ) {
        let url = URL(string: imageURL as String)
        let data = try? Data(contentsOf: url!)
        guard let colors = UIImage(data: data!)?.getColors() else {
          return successCallback(["#00000000", "#00000000", "#00000000", "#00000000", "#00000000"])
        }
        successCallback([
          colors.primaryHex,
          colors.secondaryHex,
          colors.backgroundHex,
          colors.detailHex
        ])
    }
}

Make Pull request please and we will workaround android :) Thanks!

Hi @matinzd ,
I currently haven't any setup to create a proper PR but I have this .patch file with maybe can help?

rn-dominant-color+1.4.3.patch.zip

Do you have any articles about setting up a RN Project to do PRs for external libraries?

Anything about getting color from local images?

+1

Hello, any news?

I am also waiting please give some update

Working on issue in new branch. When it was stable we are going to publish thanks to @Psiiirus

Anything?

Hello, please any update on the yet?

2 years gone and still, guess it's abandoned