Skip to content

Commit

Permalink
Merge pull request #6 from ArcBlock/color-func
Browse files Browse the repository at this point in the history
chore: add color func
  • Loading branch information
karthuszY authored Jun 5, 2023
2 parents 8fd7ae5 + 196c361 commit 0dd3c2d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Sources/did-motif-ios/DIDMotifUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public extension DIDMotifUtils {
return (colorIndex, positionIndexes)
}

/// 获取DID Motif的绘制信息
/// - Parameter did: DID
/// - Returns: (colorIndex, [cordinateIndex], DIDMotifShage)
public static func getMotifInfo(did: String) -> (Int, [Int], DIDMotifShage) {
let (colorIndex, positionIndexs) = getMotifIndexs(did: did)
let roleType = did.roleType()
Expand All @@ -65,6 +68,30 @@ public extension DIDMotifUtils {

return (colorIndex, positionIndexs, shape)
}

/// 根据 DID 获取 Motif的主色
/// - Parameter did: DID
/// - Returns: 颜色
static func getDIDMotifColor(did: String) -> UIColor {
let decoded = Data(multibaseEncoded: did.removeDIDPrefix())?.bytes.compactMap({ Int($0) }) ?? []
let errDefaultColor = UIColor.white

guard decoded.count == 26 else {
return errDefaultColor
}

// 剔除前 2 个字节 (DID Type),此时还剩 24 bytes,用来随机背景色和位置
let striped = decoded.suffix(from: 2)

// 对前 8 个 bytes 的每个 byte 求和,对 32 求模,结果为 colorIndex
let colorIndex = striped.prefix(8).reduce(0, +) % 32

guard let hexColorStr = Self.backgroundColors.objectAtIndexSafely(index: colorIndex) else {
return errDefaultColor
}

return UIColor(hex: hexColorStr)
}

static func testCase() {
print(getMotifIndexs(did: "zNKtCNqYWLYWYW3gWRA1vnRykfCBZYHZvzKr") == (4, [50, 9, 49, 9, 16, 46, 15, 18]))
Expand Down

0 comments on commit 0dd3c2d

Please sign in to comment.