Skip to content

Commit

Permalink
Move hexToColor and colorToHex out of theme class. Cleanup the fallou…
Browse files Browse the repository at this point in the history
…t. Most of the uses of hexToColor were unneeded.
  • Loading branch information
mdsitton committed Sep 15, 2014
1 parent cfb7593 commit 8b96810
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 67 deletions.
8 changes: 4 additions & 4 deletions fofix/core/Settings.py
Expand Up @@ -652,8 +652,8 @@ def __init__(self, engine):
self.opt_text_y = .14


self.opt_text_color = self.engine.theme.hexToColor(self.engine.theme.opt_text_colorVar)
self.opt_selected_color = self.engine.theme.hexToColor(self.engine.theme.opt_selected_colorVar)
self.opt_text_color = self.engine.theme.opt_text_colorVar
self.opt_selected_color = self.engine.theme.opt_selected_colorVar

if self.opt_text_color == None:
self.opt_text_color = (1,1,1)
Expand Down Expand Up @@ -1133,8 +1133,8 @@ def __init__(self, engine):
self.opt_text_y = .14


self.opt_text_color = self.engine.theme.hexToColor(self.engine.theme.opt_text_colorVar)
self.opt_selected_color = self.engine.theme.hexToColor(self.engine.theme.opt_selected_colorVar)
self.opt_text_color = self.engine.theme.opt_text_colorVar
self.opt_selected_color = self.engine.theme.opt_selected_colorVar

if self.opt_text_color == None:
self.opt_text_color = (1,1,1)
Expand Down
102 changes: 50 additions & 52 deletions fofix/core/Theme.py
Expand Up @@ -70,6 +70,35 @@ def valign(value, default='middle'):
Log.warn('Invalid vertical alignment value - defaulting to %s' % default)
return valign(default)


def hexToColor(color):
''' Convert hexadecimal color string to tuple containing rgb or rgba values '''

if not isinstance(color, str):
raise TypeError('Invalid input type: {}'.format(type(color)))

elif color[0] != '#':
raise ValueError('Invalid color')

else:
color = color[1:]

if len(color) < 4:
colorData = [color[i]+color[i] for i in xrange(0, len(color))]
else:
colorData = [color[i:i+2] for i in xrange(0, len(color), 2)]

rgbColor = tuple([int(i, 16) / 255 for i in colorData])

return rgbColor

def colorToHex(color):
if not isinstance(color, tuple):
raise TypeError

colorData = [ "%02x" % int(c * 255) for c in color]
return "#%s" % "".join(colorData)

class Theme(Task):

def __getattr__(self, attr):
Expand Down Expand Up @@ -114,12 +143,20 @@ def get(value, type = str, default = None):
if type == bool:
return isTrue(self.config.get("theme", value).lower())
elif type == "color":
return self.hexToColor(self.config.get("theme", value))
try:
value = hexToColor(self.config.get("theme", value))
except ValueError:
value = self.config.get("theme", value)

return value
else:
return type(self.config.get("theme", value))

if type == "color":
return self.hexToColor(default)
try:
value = hexToColor(default)
except ValueError:
value = default
return value
return default

#These colors are very important
Expand Down Expand Up @@ -153,19 +190,20 @@ def get(value, type = str, default = None):
#since the original Frets on Fire. What glow_color allows you to do is set it so
#the glow is either the color of the fret it's over or it can be the color the image
#actually is (if the image is white then no matter what key is hit the glow will be white)
self.hitGlowColor = get("hit_glow_color", str, "frets")

self.hitGlowColor = get("hit_glow_color", str, "frets")
if not self.hitGlowColor == "frets":
self.hitGlowColor = self.hexToColor(self.hitGlowColor)
self.hitGlowColor = hexToColor(self.hitGlowColor)

#Sets the color of the glow.png
self.glowColor = get("glow_color", str, "frets")
if not self.glowColor == "frets":
self.glowColor = self.hexToColor(self.glowColor)
self.glowColor = hexToColor(self.glowColor)

#Acts similar to the glowColor but its does so for flames instead
self.flamesColor = get("flames_color", str, "frets")
if not self.flamesColor == "frets":
self.flamesColor = self.hexToColor(self.flamesColor)
self.flamesColor = hexToColor(self.flamesColor)

#Note Colors (this applies to frets and notes)
#default is green, red, yellow, blue, orange, purple (I don't know why there's a 6th color)
Expand All @@ -180,7 +218,7 @@ def get(value, type = str, default = None):
#Color of the tails when whammied, default is set to the colors of the frets
self.killNoteColor = get("fretK_color", str, "frets")
if not self.killNoteColor == "frets":
self.killNoteColor = self.hexToColor(self.killNoteColor)
self.killNoteColor = hexToColor(self.killNoteColor)

#just like glow_color, this allows you to have tails use either the color of the note
#or the actual color of the tail
Expand Down Expand Up @@ -723,51 +761,11 @@ def setBaseColor(self, alpha = 1.0):
glColor4f(*(self.baseColor + (alpha,)))

def hexToColorResults(self, color):
if isinstance(color, tuple):
return color
elif color is None:
# TODO - Go through GameResultsScene and remove the usage of this.
try:
return hexToColor(color)
except ValueError, TypeError:
return self.baseColor
color = color.strip()
if color[0] == "#":
color = color[1:]
if len(color) == 3:
return (int(color[0], 16) / 15.0, int(color[1], 16) / 15.0, int(color[2], 16) / 15.0)
return (int(color[0:2], 16) / 255.0, int(color[2:4], 16) / 255.0, int(color[4:6], 16) / 255.0)
return self.baseColor

@staticmethod
def hexToColor(color):
if isinstance(color, tuple):
return color
elif color is None:
return (0,0,0)
if color[0] == "#":
color = color[1:]
if len(color) == 3:
return (int(color[0], 16) / 15.0, int(color[1], 16) / 15.0, int(color[2], 16) / 15.0)
elif len(color) == 4:
return (int(color[0], 16) / 15.0, int(color[1], 16) / 15.0, int(color[2], 16) / 15.0, int(color[3], 16) / 15.0)
elif len(color) == 8:
return (int(color[0:2], 16) / 255.0, int(color[2:4], 16) / 255.0, int(color[4:6], 16) / 255.0, int(color[6:8], 16) / 255.0)
return (int(color[0:2], 16) / 255.0, int(color[2:4], 16) / 255.0, int(color[4:6], 16) / 255.0)
elif color.lower() == "off":
return (-1, -1, -1)
elif color.lower() == "fret":
return (-2, -2, -2)
return (0, 0, 0)

def rgbToColor(self, color):
retVal = []
for c in color:
if isinstance(c, int) and c > 1:
retVal.append(float(c)/255.0)
return tuple(retVal)

@staticmethod
def colorToHex(color):
if isinstance(color, str):
return color
return "#" + ("".join(["%02x" % int(c * 255) for c in color]))

def packTupleKey(self, key, type = str):
vals = key.split(',')
Expand Down
4 changes: 2 additions & 2 deletions fofix/game/MainMenu.py
Expand Up @@ -152,8 +152,8 @@ def __init__(self, engine):
]

self.opt_bkg_size = [float(i) for i in self.engine.theme.opt_bkg_size]
self.opt_text_color = self.engine.theme.hexToColor(self.engine.theme.opt_text_colorVar)
self.opt_selected_color = self.engine.theme.hexToColor(self.engine.theme.opt_selected_colorVar)
self.opt_text_color = self.engine.theme.opt_text_colorVar
self.opt_selected_color = self.engine.theme.opt_selected_colorVar

if self.BGText:
strCareer = ""
Expand Down
1 change: 1 addition & 0 deletions fofix/game/Song.py
Expand Up @@ -32,6 +32,7 @@
import hashlib
import binascii

from fofix.core.Theme import hexToColor, colorToHex
from fofix.core.Unicode import utf8
from fofix.core.Language import _
from fofix.core.Theme import *
Expand Down
10 changes: 5 additions & 5 deletions fofix/game/guitarscene/GuitarScene.py
Expand Up @@ -1443,11 +1443,11 @@ def __init__(self, engine, libraryName, songName):
#MFH - new theme.ini color options:

self.ingame_stats_color = self.engine.theme.ingame_stats_colorVar
self.pause_text_color = self.engine.theme.hexToColor(self.engine.theme.pause_text_colorVar)
self.pause_selected_color = self.engine.theme.hexToColor(self.engine.theme.pause_selected_colorVar)
self.fail_text_color = self.engine.theme.hexToColor(self.engine.theme.fail_text_colorVar)
self.fail_selected_color = self.engine.theme.hexToColor(self.engine.theme.fail_selected_colorVar)
self.fail_completed_color = self.engine.theme.hexToColor(self.engine.theme.fail_completed_colorVar)
self.pause_text_color = self.engine.theme.pause_text_colorVar
self.pause_selected_color = self.engine.theme.pause_selected_colorVar
self.fail_text_color = self.engine.theme.fail_text_colorVar
self.fail_selected_color = self.engine.theme.fail_selected_colorVar
self.fail_completed_color = self.engine.theme.fail_completed_colorVar

settingsMenu = Settings.GameSettingsMenu(self.engine, self.pause_text_color, self.pause_selected_color, players = self.playerList)
careerSettingsMenu = Settings.GameCareerSettingsMenu(self.engine, self.pause_text_color, self.pause_selected_color, players = self.playerList)
Expand Down
7 changes: 4 additions & 3 deletions fofix/game/guitarscene/Rockmeter.py
Expand Up @@ -39,6 +39,7 @@
from fofix.core.LinedConfigParser import LinedConfigParser
from fofix.core.Theme import halign, valign
from fofix.core.Image import ImgDrawing
from fofix.core.Theme import hexToColor
from fofix.core.Image import drawImage
from fofix.core.constants import *
from fofix.core import Version
Expand Down Expand Up @@ -143,7 +144,7 @@ def __init__(self, stage, section):

if self.config.has_option(section, "color"):
#color of the image (#FFFFFF is white on text, on images it is full color)
self.color = list(self.engine.theme.hexToColor(self.get("color", str, "#FFFFFF")))
self.color = list(hexToColor(self.get("color", str, "#FFFFFF")))
if len(self.color) == 3:
self.color.append(1.0)
self.r, self.g, self.b, self.a = [str(c) for c in self.color]
Expand Down Expand Up @@ -679,13 +680,13 @@ def __init__(self, layer, section):
super(Fade, self).__init__(layer, section)

#starting color
color = list(self.engine.theme.hexToColor(self.get("color", str, "#FFFFFF")))
color = list(hexToColor(self.get("color", str, "#FFFFFF")))
self.start = list(color)
if len(self.start) == 3:
self.start.append(1.0)

#the color to fade to
self.end = list(self.engine.theme.hexToColor(self.get("fadeTo", str, "#FFFFFF")))
self.end = list(hexToColor(self.get("fadeTo", str, "#FFFFFF")))
#makes sure alpha is added
if len(self.end) == 3:
color.append(1.0)
Expand Down
2 changes: 1 addition & 1 deletion fofix/game/guitarscene/instruments/Vocalist.py
Expand Up @@ -194,7 +194,7 @@ def __init__(self, engine, playerObj, editorMode = False, player = 0):
self.vocalFillupCenterY = int(self.engine.theme.vocalFillupCenterY*olFactor)
self.vocalFillupInRadius = int(self.engine.theme.vocalFillupInRadius*olFactor)
self.vocalFillupOutRadius = int(self.engine.theme.vocalFillupOutRadius*olFactor)
self.vocalFillupColor = self.engine.theme.colorToHex(self.engine.theme.vocalFillupColor)
self.vocalFillupColor = self.engine.theme.vocalFillupColor
self.vocalContinuousAvailable = self.engine.theme.vocalCircularFillup and \
None not in (self.vocalFillupCenterX, self.vocalFillupCenterY, self.vocalFillupInRadius, self.vocalFillupOutRadius, self.vocalFillupColor)
if self.vocalContinuousAvailable:
Expand Down

0 comments on commit 8b96810

Please sign in to comment.