Generate color based on strings, cryptomatte style
@@ -2,19 +2,27 @@ from NodeGraphQt.base.node import BaseNode
from NodeGraphQt.base.port import Port
from NodeGraphQt.constants import NODE_PROP
from NodeGraphQt import QtCore
-import random
+import hashlib
import copy
import time
-def rand_color(seed_type):
- seed = id(seed_type)
- random.seed(seed + 10)
- r = random.randint(50, 200)
- random.seed(seed + 5)
- g = random.randint(50, 200)
- random.seed(seed + 3421)
- b = random.randint(50, 200)
- return (r, g, b, 255)
+
+# Generate random color based on strings
+class CryptoColors(object):
+ def __init__(self):
+ self.colors = {}
+
+ def get(self, text, Min=50, Max=200):
+ if text in self.colors:
+ return self.colors[text]
+ h = hashlib.sha256(text.encode('utf-8')).hexdigest()
+ d = int('0xFFFFFFFFFFFFFFFF', 0)
+ r = int(Min + (int("0x" + h[:16], 0) / d) * (Max - Min))
+ g = int(Min + (int("0x" + h[16:32], 0) / d) * (Max - Min))
+ b = int(Min + (int("0x" + h[32:48], 0) / d) * (Max - Min))
+ a = int(Min + (int("0x" + h[48:], 0) / d) * (Max - Min))
+ self.colors[text] = (r, g, b, 255)
+ return self.colors[text]
class AutoNode(BaseNode,QtCore.QObject):
@@ -29,6 +37,7 @@ class AutoNode(BaseNode,QtCore.QObject):
self.matchTypes = [[float, int]]
self.errorColor = (200, 50, 50)
self.stopCookColor = (200, 200, 200)
+ self._cryptoColors = CryptoColors()
self.defaultColor = self.get_property("color")
self.defaultValue = None
@@ -186,8 +195,8 @@ class AutoNode(BaseNode,QtCore.QObject):
else:
current_port.DataType = value_type
- current_port.border_color = rand_color(value_type)
- current_port.color = rand_color(value_type)
+ current_port.border_color = self._cryptoColors.get(str(value_type))
+ current_port.color = self._cryptoColors.get(str(value_type))
conn_type = 'multi' if current_port.multi_connection() else 'single'
data_type_name = value_type.__name__ if value_type else "all"
current_port.view.setToolTip('{}: {} ({}) '.format(current_port.name(), data_type_name, conn_type))
