Skip to content

Instantly share code, notes, and snippets.

@HT154
Last active May 25, 2023 10:13
Show Gist options
  • Save HT154/4436ea0d32a5fc8b5ca8911ab4d668f9 to your computer and use it in GitHub Desktop.
Save HT154/4436ea0d32a5fc8b5ca8911ab4d668f9 to your computer and use it in GitHub Desktop.
A better way to store colors in a Realm database. Add a Color property to a Realm model the same way as a numeric value type (including RealmOptional). This could be cleaned up once Realm supports signed integers.
//
// RealmColor.swift
//
// Created by HT154 on 11/19/16.
// Copyright © 2016 HT154. All rights reserved.
//
import Cocoa
import RealmSwift
import SpriteKit
public typealias Color = Int32
extension Color {
private var uint: UInt32 {
get { return UInt32(bitPattern: self) }
set { self = Int32(bitPattern: newValue) }
}
private init(_ raw: UInt32) {
self = Int32(bitPattern: raw)
}
public init(_ r: UInt8, _ g: UInt8, _ b: UInt8, _ a: UInt8 = 0xff) {
self.init((UInt32(r) << 24) | (UInt32(g) << 16) | (UInt32(b) << 8) | (UInt32(a) << 0))
}
public var r: UInt8 {
get { return UInt8(uint >> 24) }
set { uint = (uint & 0x00ffffff) | (UInt32(newValue) << 24) }
}
public var g: UInt8 {
get { return UInt8(uint >> 16) }
set { uint = (uint & 0xff00ffff) | (UInt32(newValue) << 16) }
}
public var b: UInt8 {
get { return UInt8(uint >> 8) }
set { uint = (uint & 0xffff00ff) | (UInt32(newValue) << 8) }
}
public var a: UInt8 {
get { return UInt8(uint >> 0) }
set { uint = (uint & 0xffffff00) | (UInt32(newValue) << 0) }
}
public var hexCode: String {
get {
return "#" + String(format: "%02dx%02dx%02dx", r, g, b) + (a == 0xff ? "" : String(format: "%02dx", a))
}
set {
var hex = newValue.trimmingCharacters(in: .whitespacesAndNewlines)
if hex.hasPrefix("#") {
hex = hex.substring(from: hex.index(after: hex.startIndex))
}
if (hex.range(of: "(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)|(^[0-9A-Fa-f]{8}$)|(^[0-9A-Fa-f]{4}$)", options: [.regularExpression]) != nil) {
var r: UInt8, g: UInt8, b: UInt8, a: UInt8
if hex.characters.count > 4 {
r = UInt8(Scanner(string: hex[0..<2]).scanHexInt()!) & 0xff
g = UInt8(Scanner(string: hex[2..<4]).scanHexInt()!) & 0xff
b = UInt8(Scanner(string: hex[4..<6]).scanHexInt()!) & 0xff
if hex.characters.count % 4 == 0 {
a = UInt8(Scanner(string: hex[6..<8]).scanHexInt()!) & 0xff
} else {
a = 0xff
}
} else {
r = UInt8(Scanner(string: hex[0]).scanHexInt()!) & 0xf
g = UInt8(Scanner(string: hex[1]).scanHexInt()!) & 0xf
b = UInt8(Scanner(string: hex[2]).scanHexInt()!) & 0xf
if hex.characters.count % 4 == 0 {
a = UInt8(Scanner(string: hex[3]).scanHexInt()!) & 0xf
} else {
a = 0xf
}
r |= r << 4; g |= g << 4; b |= b << 4; a |= a << 4
}
self = Color((UInt32(r) << 24) | (UInt32(g) << 16) | (UInt32(b) << 8) | (UInt32(a) << 0))
}
}
}
public var skColor: SKColor {
return SKColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment