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

时间:2023-01-05 18:51:39

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. Any help would be appreciated, thanks.

我找到了几个关于如何使用颜色十六进制代码创建UIColor的解决方案,但我不知道如何从上面的字符串生成颜色十六进制代码(#ffffff)。任何帮助将不胜感激,谢谢。

2 个解决方案

#1


11  

There are far too many user ids to get a unique color for every possible user 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可以为每个可能的用户ID获取唯一的颜色。您最好的选择是找到一种方法将每个用户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相同。

#2


0  

As of my understanding, you're trying to generate a random colour each time for every user and store it in the Firebase? Correct me if I'm wrong.

据我所知,您每次都试图为每个用户生成一个随机颜色并将其存储在Firebase中?如我错了请纠正我。

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. If it doesn't exist then use it!

我希望这个解决方案能解决你每次创建随机颜色的问题。简而言之,无论何时何地您需要随机的唯一颜色 - 调用该方法并检查返回的UIColor是否已存在于数据库中。如果它不存在则使用它!

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.

我希望来自Oscar De Moya的上述解决方案来自https://classictutorials.com/2014/08/generate-a-random-color-in-swift/对您有所帮助。

#1


11  

There are far too many user ids to get a unique color for every possible user 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可以为每个可能的用户ID获取唯一的颜色。您最好的选择是找到一种方法将每个用户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相同。

#2


0  

As of my understanding, you're trying to generate a random colour each time for every user and store it in the Firebase? Correct me if I'm wrong.

据我所知,您每次都试图为每个用户生成一个随机颜色并将其存储在Firebase中?如我错了请纠正我。

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. If it doesn't exist then use it!

我希望这个解决方案能解决你每次创建随机颜色的问题。简而言之,无论何时何地您需要随机的唯一颜色 - 调用该方法并检查返回的UIColor是否已存在于数据库中。如果它不存在则使用它!

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.

我希望来自Oscar De Moya的上述解决方案来自https://classictutorials.com/2014/08/generate-a-random-color-in-swift/对您有所帮助。