Last active
          November 14, 2021 09:25 
        
      - 
      
 - 
        
Save thebne/5585619dcd1143c56f17669341585577 to your computer and use it in GitHub Desktop.  
    Convert HTML color (#ffff00 or rgb(...) or rgba(...)) to Unity new Color() format
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def h2f(h): | |
| h = h.lstrip('#') | |
| return tuple(int(h[i:i+2], 16) / 255 for i in (0, 2, 4)) | |
| def r2f(r): | |
| r = r.lstrip("rgb").lstrip("a").lstrip("(").rstrip(")").strip() | |
| return [float(x.strip()) / (255 if i < 3 else 1) for i, x in enumerate(r.split(", "))] | |
| def c2u(s): | |
| f = r2f(s) if "rgb" in s else h2f(s) | |
| return f"new Color({', '.join(['{0:g}f'.format(round(x, 2)) for x in f])})" | |
| >>> print(c2u("#ff0a73")) # new Color(1, 0.04, 0.45) | |
| >>> print(c2u("rgba(50, 25, 255, .9)")) # new Color(0.2f, 0.1f, 1f, 0.9f) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment