Skip to content

Instantly share code, notes, and snippets.

@Kashif-E
Created September 3, 2023 15:14
Show Gist options
  • Save Kashif-E/c135390043f6bd7b685406245d6f9d69 to your computer and use it in GitHub Desktop.
Save Kashif-E/c135390043f6bd7b685406245d6f9d69 to your computer and use it in GitHub Desktop.
Material Color to UIColor
val uiColor = MaterialTheme.colors.surface.toUIColor()
@OptIn(ExperimentalForeignApi::class)
fun Color.toUIColor(): UIColor {
val colorSpace = CGColorSpaceCreateDeviceRGB()
val components = nativeHeap.allocArray<DoubleVar>(4)
components[0] = red.toDouble()
components[1] = green.toDouble()
components[2] = blue.toDouble()
components[3] = alpha.toDouble()
val cgColor = CGColorCreate(colorSpace, components)
return UIColor.colorWithCGColor(cgColor)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment