1

I have a situation in an iOS Swift app where the user can "customize the screen" by adding other views and/or changing the view's background color. When they tap save, I want to store the attributes of each object, as well as the parent view's background color to a MySQL database for later retrieval and reconstruction of the views. All data updates are done through PHP REST services.

I'm currently struggling with the color data for the background color. If I print() the color, I get something like "UIDeviceRGBColorSpace 0.866667 0.92549 1 1". I can also convert it to NSData with the following:

let data = NSKeyedArchiver.archivedDataWithRootObject(self.view.backgroundColor!)

However, in either case, I have no idea how to save the data to the database via PHP REST service or even what datatype I would use.

Am I going down the wrong path, altogether? Should I be doing something like grab the RGB values and Alpha and save those to 4 attributes in the database, or get the hex value and store that? Perhaps, there is yet a different approach that would be even more straight forward?

Lastmboy
  • 1,849
  • 3
  • 21
  • 39
  • RGB would be better, you can then reuse the same data for Android or Web or any other platform. – ogres May 06 '16 at 08:34
  • You could store them as hex using the standard HTML style representation. – Chris May 06 '16 at 08:34
  • Thanks, ogres. What is the best way to obtain the RGB values? Convert to CIColor? It looks like you can get the RGB individual values from that. – Lastmboy May 06 '16 at 08:36
  • 1
    Possible duplicate of [How to get RGB values from UIColor?](http://stackoverflow.com/questions/437113/how-to-get-rgb-values-from-uicolor) – Wez May 06 '16 at 09:09
  • Thanks, @Wezly. That one is in Objective-C, rather than Swift. However, I just found the Swift solution in [Extract RGB Values From UIColor](http://stackoverflow.com/questions/28532564/extract-rgb-values-from-uicolor). – Lastmboy May 06 '16 at 09:17
  • That's not a very good general solution to the problem – that discards information (the colour may for one not be in the RGB space and may have needed to be converted to it, which is not a theoretical concern – could for instance be a grayscale value depending on how it was generated). I'm also not sure if colour calibration related data is retainable for instance with the NSCoding based data (which you certainly would again lose in this case). – mz2 May 06 '16 at 10:28
  • Toning down my comment a bit – it is of course a good general real-world solution to extract RGB components in the case you actually need to decode the colour information with any other non-Apple device :-) – mz2 May 06 '16 at 10:43

1 Answers1

0

If you care about retaining all the information encoded in that NSColor / UIColor instance (original precise value in its original colour space which may not be RGB, calibration etc) and need to encode it as a string (instead of for instance binary blob, which is possible with MySQL too), you could use base64 encoding – send your server the colour in base64 encoded form, and decode it back when retrieving.

let color = NSColor.blackColor()
let data = NSKeyedArchiver.archivedDataWithRootObject(color)

// this can go in your database
let base64EncodedColorString = data.base64EncodedStringWithOptions([])

let decodedColorData = NSData(base64EncodedString: base64EncodedColorString, options: [])

NSKeyedUnarchiver.unarchiveObjectWithData(decodedColorData!)

This has the obvious downside that your server will have no clue about what that data encodes, so if you care about that, then getting the colour's RGB components and storing those as a string is probably the better option.

Community
  • 1
  • 1
mz2
  • 4,672
  • 1
  • 27
  • 47