简体   繁体   English

Swift:将字符串转换为十六进制颜色代码

[英]Swift: Convert string to hex color code

I have generated user Ids (with a format like ""XzSYoKJaqKYREkdB2dgwt0fLOPP2"") from a database that I would like to use to create an UIColor.我已经从我想用来创建 UIColor 的数据库中生成了用户 ID(格式类似于“XzSYoKJaqKYREkdB2dgwt0fLOPP2”)。

I found several solutions on how to crate an UIColor with a color hex code, but I don't quite know how I would generate a color hex code (#ffffff) from a string like the one above.我找到了几种关于如何使用颜色十六进制代码创建 UIColor 的解决方案,但我不太清楚如何从像上面那样的字符串生成颜色十六进制代码 (#ffffff)。 Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

There are far too many user ids to get a unique color for every possible user id.有太多的用户 ID,无法为每个可能的用户 ID 获得唯一的颜色。 Your best option would be to find a way to narrow down each user id to one of the possible available colors and accept the fact that two users may have the same color.您最好的选择是找到一种方法将每个用户 ID 缩小到一种可能的可用颜色,并接受两个用户可能具有相同颜色的事实。

One possible solution is would be to get the hashValue of the user id string and then reduce that Int down to one of the possible 16,777,216 colors.一种可能的解决方案是获取用户 ID 字符串的hashValue ,然后将该Int减少到可能的 16,777,216 种颜色之一。

let userId = "XzSYoKJaqKYREkdB2dgwt0fLOPP2" // or whatever the id is
let hash = abs(userId.hashValue)
let colorNum = hash % (256*256*256)

At this point colorNum is the range 0 - 0xFFFFFF此时colorNum的范围是0 - 0xFFFFFF

You can now create a color from colorNum .您现在可以从colorNum创建颜色。

let red = colorNum >> 16
let green = (colorNum & 0x00FF00) >> 8
let blue = (colorNum & 0x0000FF)
let userColor = UIColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)

You will want to store this color in the user's profile since the hashValue isn't guaranteed to be the same each time your app runs.您需要将此颜色存储在用户的配置文件中,因为无法保证每次应用运行时hashValue都相同。

As of my understanding, you're trying to generate a random colour each time for every user and store it in the Firebase?据我了解,您每次都尝试为每个用户生成随机颜色并将其存储在 Firebase 中? Correct me if I'm wrong.如果我错了纠正我。

I hope this solution would fix your issue of creating random colours each time.我希望这个解决方案可以解决您每次创建随机颜色的问题。 In short, wherever and whenever you need a random unique colour - call the method and check if the returned UIColor already exists in the database.简而言之,无论何时何地,您都需要随机的唯一颜色 - 调用该方法并检查返回的 UIColor 是否已存在于数据库中。 If it doesn't exist then use it!如果它不存在,则使用它!

func getRandomColor() -> UIColor {
let randomRed:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
let randomGreen:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
let randomBlue:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}

I hope the above solution from Oscar De Moya's from https://classictutorials.com/2014/08/generate-a-random-color-in-swift/ would help you.我希望来自https://classictutorials.com/2014/08/generate-a-random-color-in-swift/ 的Oscar De Moya 的上述解决方案会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM