Skip to content

Instantly share code, notes, and snippets.

@wcoder
Created March 19, 2019 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wcoder/de1b26b050ff5d31576c68b416102f95 to your computer and use it in GitHub Desktop.
Save wcoder/de1b26b050ff5d31576c68b416102f95 to your computer and use it in GitHub Desktop.
Port of UIColorMSHashExtension from Swift to C#. Office UI Fabric iOS for Xamarin.iOS
using System;
using UIKit;
namespace XamarinOfficeUIFabric
{
public static class ColorExtensions
{
// Original MS Source:
// https://github.com/OfficeDev/office-ui-fabric-ios/blob/master/OfficeUIFabricCore/OfficeUIFabricCore/Core/Colors/UIColorMSHashExtension.swift
public static UIColor MSHashColor(this string hash)
{
var randomNum = 3074457345618258791;
var rgbRed = new nfloat[] { 0, 8, 16, 136, 180, 232, 218, 0, 0, 0, 168, 78 };
var rgbGreen = new nfloat[] { 120, 130, 124, 23, 0, 17, 59, 111, 94, 78, 0, 37 };
var rgbBlue = new nfloat[] { 215, 114, 16, 152, 158, 35, 1, 148, 80, 140, 0, 127 };
foreach (var c in hash)
{
randomNum = randomNum + c;
randomNum = randomNum * 3074457345618258799;
}
var index = randomNum % rgbBlue.Length;
return UIColor.FromRGBA(
red: rgbRed[index] / 255,
green: rgbGreen[index] / 255,
blue: rgbBlue[index] / 255,
alpha: 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment