Example #1
0
def newtheme(widget, oldstyle):
    global surface

    lnewcolor = widget.get_style().bg[gtk.STATE_NORMAL]
    dnewcolor = widget.get_style().dark[gtk.STATE_NORMAL]
    if oldstyle:
        loldcolor = oldstyle.bg[gtk.STATE_NORMAL]
        doldcolor = oldstyle.dark[gtk.STATE_NORMAL]
        if lnewcolor.red   == loldcolor.red and \
           lnewcolor.green == loldcolor.green and \
           lnewcolor.blue  == loldcolor.blue and \
           dnewcolor.red   == doldcolor.red and \
           dnewcolor.green == doldcolor.green and \
           dnewcolor.blue  == doldcolor.blue:
            return

    colors = [
        lnewcolor.red / 256, lnewcolor.green / 256, lnewcolor.blue / 256,
        dnewcolor.red / 256, dnewcolor.green / 256, dnewcolor.blue / 256
    ]

    # Check if a cache has been saved
    temppng = addUserCachePrefix("temp.png")
    if path.isfile(temppng):
        f = open(temppng, "rb")
        # Check if the cache was made while using the same theme
        if [ord(c) for c in f.read(6)] == colors:
            surface = cairo.ImageSurface.create_from_png(f)
            return

    # Get mostly transparant shadowy image
    imgsurface = cairo.ImageSurface.create_from_png(CLEARPATH)
    AVGALPHA = 108 / 255.

    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, imgsurface.get_width(),
                                 imgsurface.get_height())
    ctx = cairo.Context(surface)
    if lnewcolor.blue - dnewcolor.blue > 0:
        a = dnewcolor.red / (3 * (lnewcolor.blue - dnewcolor.blue) *
                             (1 - AVGALPHA))
        ctx.set_source_rgb(
            lnewcolor.red / 65535. / 2 + dnewcolor.red / 65535. * a / 2,
            lnewcolor.green / 65535. / 2 + dnewcolor.green / 65535. * a / 2,
            lnewcolor.blue / 65535. / 2 + dnewcolor.blue / 65535. * a / 2)
        ctx.paint()
    ctx.set_source_surface(imgsurface, 0, 0)
    ctx.paint_with_alpha(.8)

    # Save a cache for later use. Save 'newcolor' in the frist three pixels
    # to check for theme changes between two instances
    f = open(temppng, "wb")
    for color in colors:
        f.write(chr(color))
    surface.write_to_png(f)
Example #2
0
def newtheme (widget, oldstyle):
    global surface
    
    lnewcolor = widget.get_style().bg[gtk.STATE_NORMAL]
    dnewcolor = widget.get_style().dark[gtk.STATE_NORMAL]
    if oldstyle:
        loldcolor = oldstyle.bg[gtk.STATE_NORMAL]
        doldcolor = oldstyle.dark[gtk.STATE_NORMAL]
        if lnewcolor.red   == loldcolor.red and \
           lnewcolor.green == loldcolor.green and \
           lnewcolor.blue  == loldcolor.blue and \
           dnewcolor.red   == doldcolor.red and \
           dnewcolor.green == doldcolor.green and \
           dnewcolor.blue  == doldcolor.blue:
            return
    
    colors = [
        lnewcolor.red/256, lnewcolor.green/256, lnewcolor.blue/256,
        dnewcolor.red/256, dnewcolor.green/256, dnewcolor.blue/256
    ]
    
    # Check if a cache has been saved
    temppng = addUserCachePrefix("temp.png")
    if path.isfile(temppng):
        f = open(temppng, "rb")
        # Check if the cache was made while using the same theme
        if [ord(c) for c in f.read(6)] == colors:
            surface = cairo.ImageSurface.create_from_png(f)
            return
    
    # Get mostly transparant shadowy image
    imgsurface = cairo.ImageSurface.create_from_png(CLEARPATH)
    AVGALPHA = 108/255.
    
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24,
            imgsurface.get_width(), imgsurface.get_height())
    ctx = cairo.Context (surface)
    if lnewcolor.blue-dnewcolor.blue > 0:
        a = dnewcolor.red/(3*(lnewcolor.blue-dnewcolor.blue)*(1-AVGALPHA))
        ctx.set_source_rgb(
            lnewcolor.red/65535./2  + dnewcolor.red/65535.*a/2,
            lnewcolor.green/65535./2 + dnewcolor.green/65535.*a/2,
            lnewcolor.blue/65535./2  + dnewcolor.blue/65535.*a/2)
        ctx.paint()
    ctx.set_source_surface(imgsurface, 0, 0)
    ctx.paint_with_alpha(.8)
    
    # Save a cache for later use. Save 'newcolor' in the frist three pixels
    # to check for theme changes between two instances
    f = open(temppng, "wb")
    for color in colors:
        f.write(chr(color))
    surface.write_to_png(f)
Example #3
0
def newTheme(widget, background=None):
    global surface, provider, loldcolor, doldcolor

    style_ctxt = widget.get_style_context()

    # get colors from theme

    # bg color
    found, bgcol = style_ctxt.lookup_color("bg_color")
    if not found:
        found, bgcol = style_ctxt.lookup_color("theme_bg_color")
        if not found:
            # fallback value
            bgcol = Gdk.RGBA(red=0.929412,
                             green=0.929412,
                             blue=0.929412,
                             alpha=1.0)

    # bg selected color
    found, bgsel = style_ctxt.lookup_color("theme_selected_bg_color")
    if not found:
        # fallback value
        bgsel = Gdk.RGBA(red=0.290, green=0.565, blue=0.851, alpha=1.0)

    # fg color
    found, fgcol = style_ctxt.lookup_color("fg_color")
    if not found:
        found, fgcol = style_ctxt.lookup_color("theme_fg_color")
        if not found:
            fgcol = Gdk.RGBA(red=0.180392,
                             green=0.203922,
                             blue=0.211765,
                             alpha=1.000000)

    # base color
    found, basecol = style_ctxt.lookup_color("base_color")
    if not found:
        found, basecol = style_ctxt.lookup_color("theme_base_color")
        if not found:
            basecol = Gdk.RGBA(red=0.929412,
                               green=0.929412,
                               blue=0.929412,
                               alpha=1.0)

    # text color
    found, textcol = style_ctxt.lookup_color("text_color")
    if not found:
        found, textcol = style_ctxt.lookup_color("theme_text_color")
        if not found:
            textcol = Gdk.RGBA(red=0.180392,
                               green=0.203922,
                               blue=0.211765,
                               alpha=1.0)

    def get_col(col, mult):
        red = col.red * mult
        green = col.green * mult
        blue = col.blue * mult
        if red > 1.0:
            red = 1.0
        if green > 1.0:
            green = 1.0
        if blue > 1.0:
            blue = 1.0
        if red == 1 and green == 1 and blue == 1:
            return Gdk.RGBA(0.99, 0.99, 0.99, 1.0)
        else:
            return Gdk.RGBA(red, green, blue, 1.0)

    # derive other colors
    bgacol = get_col(bgcol, 0.9)                # bg_active
    dcol = get_col(bgcol, 0.7)                  # dark
    darksel = get_col(bgsel, 0.71)              # dark selected
    dpcol = get_col(bgcol, 0.71)                # dark prelight
    dacol = get_col(dcol, 0.9)                  # dark_active
    lcol = get_col(bgcol, 1.3)                  # light color
    lightsel = get_col(bgsel, 1.3)              # light selected
    fgsel = Gdk.RGBA(0.99, 0.99, 0.99, 1.0)     # fg selected
    fgpcol = get_col(fgcol, 1.054)              # fg prelight
    fgacol = Gdk.RGBA(0.0, 0.0, 0.0, 1.0)       # fg active
    textaacol = Gdk.RGBA(
        min(
            (basecol.red + textcol.red) / 2., 1.0), min(
                (basecol.green + textcol.green) / 2., 1.0), min(
                    (basecol.blue + textcol.blue) / 2., 1.0))  # text_aa

    data = "@define-color p_bg_color " + hexcol(bgcol) + ";" \
        "@define-color p_bg_prelight " + hexcol(bgcol) + ";" \
        "@define-color p_bg_active " + hexcol(bgacol) + ";" \
        "@define-color p_bg_selected " + hexcol(bgsel) + ";" \
        "@define-color p_bg_insensitive " + hexcol(bgcol) + ";" \
        "@define-color p_base_color " + hexcol(basecol) + ";" \
        "@define-color p_dark_color " + hexcol(dcol) + ";" \
        "@define-color p_dark_prelight " + hexcol(dpcol) + ";" \
        "@define-color p_dark_active " + hexcol(dacol) + ";" \
        "@define-color p_dark_selected " + hexcol(darksel) + ";" \
        "@define-color p_text " + hexcol(textcol) + ";" \
        "@define-color p_text_aa " + hexcol(textaacol) + ";" \
        "@define-color p_light_color " + hexcol(lcol) + ";" \
        "@define-color p_light_selected " + hexcol(lightsel) + ";" \
        "@define-color p_fg_color " + hexcol(fgcol) + ";" \
        "@define-color p_fg_prelight " + hexcol(fgpcol) + ";" \
        "@define-color p_fg_selected " + hexcol(fgsel) + ";" \
        "@define-color p_fg_active " + hexcol(fgacol) + ";"

    if provider is not None:
        style_ctxt.remove_provider_for_screen(Gdk.Screen.get_default(), provider)

    provider = Gtk.CssProvider.new()
    provider.load_from_data(data.encode())
    style_ctxt.add_provider_for_screen(Gdk.Screen.get_default(), provider,
                                       Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    lnewcolor = bgcol
    dnewcolor = dcol

    # check if changed
    if loldcolor and background is None:
        if lnewcolor.red == loldcolor.red and \
           lnewcolor.green == loldcolor.green and \
           lnewcolor.blue == loldcolor.blue and \
           dnewcolor.red == doldcolor.red and \
           dnewcolor.green == doldcolor.green and \
           dnewcolor.blue == doldcolor.blue:
            return

    loldcolor = lnewcolor
    doldcolor = dnewcolor

    # global colors have been set up
    # now set colors on startup panel
    lnewcolor = style_ctxt.lookup_color("p_bg_color")[1]
    dnewcolor = style_ctxt.lookup_color("p_dark_color")[1]

    colors = [
        int(lnewcolor.red * 255), int(lnewcolor.green * 255),
        int(lnewcolor.blue * 255), int(dnewcolor.red * 255),
        int(dnewcolor.green * 255), int(dnewcolor.blue * 255)
    ]

    if background is None:
        background = conf.get("welcome_image")

    if not path.isfile(background):
        background = addDataPrefix("glade/clear.png")
        conf.set("welcome_image", background)

    if not background.endswith("clear.png"):
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(background)
        x, y, height, width = uistuff.getMonitorBounds()
        pixbuf = pixbuf.scale_simple(height, width, GdkPixbuf.InterpType.BILINEAR)
        # for frmat in GdkPixbuf.Pixbuf.get_formats():
        #     print(frmat.get_extensions())
        surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, 0, None)
        return

    # Check if a cache has been saved
    temppng = addUserCachePrefix("temp.png")
    if path.isfile(temppng):
        fyle = open(temppng, "rb")
        # Check if the cache was made while using the same theme
        if list(fyle.read(6)) == colors:
            surface = cairo.ImageSurface.create_from_png(fyle)
            return

    # Get mostly transparant shadowy image
    imgsurface = cairo.ImageSurface.create_from_png(background)
    avgalpha = 108 / 255.

    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, imgsurface.get_width(),
                                 imgsurface.get_height())
    ctx = cairo.Context(surface)
    if lnewcolor.blue * 65535 - dnewcolor.blue * 65535 > 0:
        midtone = dnewcolor.red * 65535 / (3 * (
            lnewcolor.blue * 65535 - dnewcolor.blue * 65535) * (1 - avgalpha))
        ctx.set_source_rgb(lnewcolor.red / 2 + dnewcolor.red * midtone / 2,
                           lnewcolor.green / 2 + dnewcolor.green * midtone / 2,
                           lnewcolor.blue / 2 + dnewcolor.blue * midtone / 2)
        ctx.paint()
    ctx.set_source_surface(imgsurface, 0, 0)
    ctx.paint_with_alpha(.8)

    # Save a cache for later use. Save 'newcolor' in the frist three pixels
    # to check for theme changes between two instances
    fyle = open(temppng, "wb")
    fyle.write(bytes(colors))
    surface.write_to_png(fyle)
Example #4
0
def newTheme(widget):
    global surface, provider, loldcolor, doldcolor

    style_ctxt = widget.get_style_context()

    # get colors from theme

    # bg color
    found, bgcol = style_ctxt.lookup_color("bg_color")
    if not found:
        found, bgcol = style_ctxt.lookup_color("theme_bg_color")
        if not found:
            # fallback value
            bgcol = Gdk.RGBA(red=0.929412,
                             green=0.929412,
                             blue=0.929412,
                             alpha=1.0)

    # bg selected color
    found, bgsel = style_ctxt.lookup_color("theme_selected_bg_color")
    if not found:
        # fallback value
        bgsel = Gdk.RGBA(red=0.290, green=0.565, blue=0.851, alpha=1.0)

    # fg color
    found, fgcol = style_ctxt.lookup_color("fg_color")
    if not found:
        found, fgcol = style_ctxt.lookup_color("theme_fg_color")
        if not found:
            fgcol = Gdk.RGBA(red=0.180392,
                             green=0.203922,
                             blue=0.211765,
                             alpha=1.000000)

    # base color
    found, basecol = style_ctxt.lookup_color("base_color")
    if not found:
        found, basecol = style_ctxt.lookup_color("theme_base_color")
        if not found:
            basecol = Gdk.RGBA(red=0.929412,
                               green=0.929412,
                               blue=0.929412,
                               alpha=1.0)

    # text color
    found, textcol = style_ctxt.lookup_color("text_color")
    if not found:
        found, textcol = style_ctxt.lookup_color("theme_text_color")
        if not found:
            textcol = Gdk.RGBA(red=0.180392,
                               green=0.203922,
                               blue=0.211765,
                               alpha=1.0)

    def get_col(col, mult):
        red = col.red * mult
        green = col.green * mult
        blue = col.blue * mult
        if red > 1.0:
            red = 1.0
        if green > 1.0:
            green = 1.0
        if blue > 1.0:
            blue = 1.0
        if red == 1 and green == 1 and blue == 1:
            return Gdk.RGBA(0.99, 0.99, 0.99, 1.0)
        else:
            return Gdk.RGBA(red, green, blue, 1.0)

    # derive other colors
    bgacol = get_col(bgcol, 0.9)  # bg_active
    dcol = get_col(bgcol, 0.7)  # dark
    darksel = get_col(bgsel, 0.71)  # dark selected
    dpcol = get_col(bgcol, 0.71)  # dark prelight
    dacol = get_col(dcol, 0.9)  # dark_active
    lcol = get_col(bgcol, 1.3)  # light color
    lightsel = get_col(bgsel, 1.3)  # light selected
    fgsel = Gdk.RGBA(0.99, 0.99, 0.99, 1.0)  # fg selected
    fgpcol = get_col(fgcol, 1.054)  # fg prelight
    fgacol = Gdk.RGBA(0.0, 0.0, 0.0, 1.0)  # fg active
    textaacol = Gdk.RGBA(min((basecol.red + textcol.red) / 2., 1.0),
                         min((basecol.green + textcol.green) / 2., 1.0),
                         min((basecol.blue + textcol.blue) / 2.,
                             1.0))  # text_aa

    def hexcol(color):
        """ Description : Takes a colour tuple(rgb) and returns a hex based string #rrggbb
            Returns : (str)
        """
        return "#%02X%02X%02X" % (int(color.red * 255), int(
            color.green * 255), int(color.blue * 255))

    data = "@define-color p_bg_color "          + hexcol(bgcol) + ";" \
           "@define-color p_bg_prelight "       + hexcol(bgcol) + ";" \
           "@define-color p_bg_active "         + hexcol(bgacol) + ";" \
           "@define-color p_bg_selected "       + hexcol(bgsel) + ";" \
           "@define-color p_bg_insensitive "    + hexcol(bgcol) + ";" \
           "@define-color p_base_color "        + hexcol(basecol) + ";" \
           "@define-color p_dark_color "        + hexcol(dcol) + ";" \
           "@define-color p_dark_prelight "     + hexcol(dpcol) + ";" \
           "@define-color p_dark_active "       + hexcol(dacol) + ";" \
           "@define-color p_dark_selected "     + hexcol(darksel) + ";" \
           "@define-color p_text_aa "           + hexcol(textaacol) + ";" \
           "@define-color p_light_color "       + hexcol(lcol) + ";" \
           "@define-color p_light_selected "    + hexcol(lightsel) + ";" \
           "@define-color p_fg_color "          + hexcol(fgcol) + ";" \
           "@define-color p_fg_prelight "       + hexcol(fgpcol) + ";" \
           "@define-color p_fg_selected "       + hexcol(fgsel) + ";" \
           "@define-color p_fg_active "         + hexcol(fgacol) + ";"

    if provider is not None:
        style_ctxt.remove_provider_for_screen(Gdk.Screen.get_default(),
                                              provider)

    provider = Gtk.CssProvider.new()
    provider.load_from_data(data.encode())
    style_ctxt.add_provider_for_screen(Gdk.Screen.get_default(), provider, \
                                       Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    lnewcolor = bgcol
    dnewcolor = dcol

    # check if changed
    if loldcolor:
        if lnewcolor.red == loldcolor.red and \
           lnewcolor.green == loldcolor.green and \
           lnewcolor.blue == loldcolor.blue and \
           dnewcolor.red == doldcolor.red and \
           dnewcolor.green == doldcolor.green and \
           dnewcolor.blue == doldcolor.blue:
            return

    loldcolor = lnewcolor
    doldcolor = dnewcolor

    # global colors have been set up
    # now set colors on startup panel
    lnewcolor = style_ctxt.lookup_color("p_bg_color")[1]
    dnewcolor = style_ctxt.lookup_color("p_dark_color")[1]

    colors = [
        int(lnewcolor.red * 255),
        int(lnewcolor.green * 255),
        int(lnewcolor.blue * 255),
        int(dnewcolor.red * 255),
        int(dnewcolor.green * 255),
        int(dnewcolor.blue * 255)
    ]

    # Check if a cache has been saved
    temppng = addUserCachePrefix("temp.png")
    if path.isfile(temppng):
        fyle = open(temppng, "rb")
        # Check if the cache was made while using the same theme
        if list(fyle.read(6)) == colors:
            surface = cairo.ImageSurface.create_from_png(fyle)
            return

    # Get mostly transparant shadowy image
    imgsurface = cairo.ImageSurface.create_from_png(CLEARPATH)
    avgalpha = 108 / 255.

    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, imgsurface.get_width(),
                                 imgsurface.get_height())
    ctx = cairo.Context(surface)
    if lnewcolor.blue * 65535 - dnewcolor.blue * 65535 > 0:
        midtone = dnewcolor.red * 65535 / (
            3 * (lnewcolor.blue * 65535 - dnewcolor.blue * 65535) *
            (1 - avgalpha))
        ctx.set_source_rgb(lnewcolor.red / 2 + dnewcolor.red * midtone / 2,
                           lnewcolor.green / 2 + dnewcolor.green * midtone / 2,
                           lnewcolor.blue / 2 + dnewcolor.blue * midtone / 2)
        ctx.paint()
    ctx.set_source_surface(imgsurface, 0, 0)
    ctx.paint_with_alpha(.8)

    # Save a cache for later use. Save 'newcolor' in the frist three pixels
    # to check for theme changes between two instances
    fyle = open(temppng, "wb")
    fyle.write(bytes(colors))
    surface.write_to_png(fyle)
Example #5
0
        },
        {
            "id": ARTIFICIAL,
            "name": "Chess engine(s)"
        },
        {
            "id": REMOTE,
            "name": "ICS game"
        },
    ]
    conn.execute(tag.insert(), new_values)
    conn.close()


def ini_schema_version(engine):
    conn = engine.connect()
    conn.execute(schema_version.insert(), [
        {
            "id": 1,
            "version": SCHEMA_VERSION
        },
    ])
    conn.close()


# create an empty database to use as skeleton
empty_db = os.path.join(addUserCachePrefix("%s.sqlite" % SCHEMA_VERSION))
if not os.path.isfile(empty_db):
    engine = get_engine(empty_db)
    engine.dispose()
Example #6
0
        for index in table.indexes:
            try:
                index.drop(bind=engine)
            except OperationalError as e:
                if e.orig.args[0].startswith("no such index"):
                    pass
                    # print(e.orig.args[0])
                else:
                    raise


def create_indexes(engine):
    for table in metadata.tables.values():
        for index in table.indexes:
            index.create(bind=engine)


def ini_schema_version(engine):
    conn = engine.connect()
    conn.execute(schema_version.insert(), [{"id": 1, "version": SCHEMA_VERSION}, ])
    conn.close()


# create an empty database to use as skeleton
empty_db = os.path.join(addUserCachePrefix("%s.sqlite" % SCHEMA_VERSION))
if not os.path.isfile(empty_db):
    engine = get_engine(empty_db)
    metadata.create_all(engine)
    ini_schema_version(engine)
    engine.dispose()
Example #7
0
def newTheme(widget, background=None):
    global surface, provider, loldcolor, doldcolor

    style_ctxt = widget.get_style_context()

    # get colors from theme

    # bg color
    found, bgcol = style_ctxt.lookup_color("bg_color")
    if not found:
        found, bgcol = style_ctxt.lookup_color("theme_bg_color")
        if not found:
            # fallback value
            bgcol = Gdk.RGBA(red=0.929412,
                             green=0.929412,
                             blue=0.929412,
                             alpha=1.0)

    # bg selected color
    found, bgsel = style_ctxt.lookup_color("theme_selected_bg_color")
    if not found:
        # fallback value
        bgsel = Gdk.RGBA(red=0.290, green=0.565, blue=0.851, alpha=1.0)

    # fg color
    found, fgcol = style_ctxt.lookup_color("fg_color")
    if not found:
        found, fgcol = style_ctxt.lookup_color("theme_fg_color")
        if not found:
            fgcol = Gdk.RGBA(red=0.180392,
                             green=0.203922,
                             blue=0.211765,
                             alpha=1.000000)

    # base color
    found, basecol = style_ctxt.lookup_color("base_color")
    if not found:
        found, basecol = style_ctxt.lookup_color("theme_base_color")
        if not found:
            basecol = Gdk.RGBA(red=0.929412,
                               green=0.929412,
                               blue=0.929412,
                               alpha=1.0)

    # text color
    found, textcol = style_ctxt.lookup_color("text_color")
    if not found:
        found, textcol = style_ctxt.lookup_color("theme_text_color")
        if not found:
            textcol = Gdk.RGBA(red=0.180392,
                               green=0.203922,
                               blue=0.211765,
                               alpha=1.0)

    def get_col(col, mult):
        red = col.red * mult
        green = col.green * mult
        blue = col.blue * mult
        if red > 1.0:
            red = 1.0
        if green > 1.0:
            green = 1.0
        if blue > 1.0:
            blue = 1.0
        if red == 1 and green == 1 and blue == 1:
            return Gdk.RGBA(0.99, 0.99, 0.99, 1.0)
        else:
            return Gdk.RGBA(red, green, blue, 1.0)

    # derive other colors
    bgacol = get_col(bgcol, 0.9)  # bg_active
    dcol = get_col(bgcol, 0.7)  # dark
    darksel = get_col(bgsel, 0.71)  # dark selected
    dpcol = get_col(bgcol, 0.71)  # dark prelight
    dacol = get_col(dcol, 0.9)  # dark_active
    lcol = get_col(bgcol, 1.3)  # light color
    lightsel = get_col(bgsel, 1.3)  # light selected
    fgsel = Gdk.RGBA(0.99, 0.99, 0.99, 1.0)  # fg selected
    fgpcol = get_col(fgcol, 1.054)  # fg prelight
    fgacol = Gdk.RGBA(0.0, 0.0, 0.0, 1.0)  # fg active
    textaacol = Gdk.RGBA(
        min((basecol.red + textcol.red) / 2.0, 1.0),
        min((basecol.green + textcol.green) / 2.0, 1.0),
        min((basecol.blue + textcol.blue) / 2.0, 1.0),
    )  # text_aa

    data = ("@define-color p_bg_color " + hexcol(bgcol) + ";"
            "@define-color p_bg_prelight " + hexcol(bgcol) + ";"
            "@define-color p_bg_active " + hexcol(bgacol) + ";"
            "@define-color p_bg_selected " + hexcol(bgsel) + ";"
            "@define-color p_bg_insensitive " + hexcol(bgcol) + ";"
            "@define-color p_base_color " + hexcol(basecol) + ";"
            "@define-color p_dark_color " + hexcol(dcol) + ";"
            "@define-color p_dark_prelight " + hexcol(dpcol) + ";"
            "@define-color p_dark_active " + hexcol(dacol) + ";"
            "@define-color p_dark_selected " + hexcol(darksel) + ";"
            "@define-color p_text " + hexcol(textcol) + ";"
            "@define-color p_text_aa " + hexcol(textaacol) + ";"
            "@define-color p_light_color " + hexcol(lcol) + ";"
            "@define-color p_light_selected " + hexcol(lightsel) + ";"
            "@define-color p_fg_color " + hexcol(fgcol) + ";"
            "@define-color p_fg_prelight " + hexcol(fgpcol) + ";"
            "@define-color p_fg_selected " + hexcol(fgsel) + ";"
            "@define-color p_fg_active " + hexcol(fgacol) + ";")

    if provider is not None:
        style_ctxt.remove_provider_for_screen(Gdk.Screen.get_default(),
                                              provider)

    provider = Gtk.CssProvider.new()
    provider.load_from_data(data.encode())
    style_ctxt.add_provider_for_screen(Gdk.Screen.get_default(), provider,
                                       Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    lnewcolor = bgcol
    dnewcolor = dcol

    # check if changed
    if loldcolor and background is None:
        if (lnewcolor.red == loldcolor.red
                and lnewcolor.green == loldcolor.green
                and lnewcolor.blue == loldcolor.blue
                and dnewcolor.red == doldcolor.red
                and dnewcolor.green == doldcolor.green
                and dnewcolor.blue == doldcolor.blue):
            return

    loldcolor = lnewcolor
    doldcolor = dnewcolor

    # global colors have been set up
    # now set colors on startup panel
    lnewcolor = style_ctxt.lookup_color("p_bg_color")[1]
    dnewcolor = style_ctxt.lookup_color("p_dark_color")[1]

    colors = [
        int(lnewcolor.red * 255),
        int(lnewcolor.green * 255),
        int(lnewcolor.blue * 255),
        int(dnewcolor.red * 255),
        int(dnewcolor.green * 255),
        int(dnewcolor.blue * 255),
    ]

    if background is None:
        background = conf.get("welcome_image")

    if not path.isfile(background):
        background = addDataPrefix("glade/clear.png")
        conf.set("welcome_image", background)

    if not background.endswith("clear.png"):
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(background)
        x, y, height, width = uistuff.getMonitorBounds()
        pixbuf = pixbuf.scale_simple(height, width,
                                     GdkPixbuf.InterpType.BILINEAR)
        # for frmat in GdkPixbuf.Pixbuf.get_formats():
        #     print(frmat.get_extensions())
        surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, 0, None)
        return

    # Check if a cache has been saved
    temppng = addUserCachePrefix("temp.png")
    if path.isfile(temppng):
        fyle = open(temppng, "rb")
        # Check if the cache was made while using the same theme
        if list(fyle.read(6)) == colors:
            surface = cairo.ImageSurface.create_from_png(fyle)
            return

    # Get mostly transparant shadowy image
    imgsurface = cairo.ImageSurface.create_from_png(background)
    avgalpha = 108 / 255.0

    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, imgsurface.get_width(),
                                 imgsurface.get_height())
    ctx = cairo.Context(surface)
    if lnewcolor.blue * 65535 - dnewcolor.blue * 65535 > 0:
        midtone = (dnewcolor.red * 65535 /
                   (3 * (lnewcolor.blue * 65535 - dnewcolor.blue * 65535) *
                    (1 - avgalpha)))
        ctx.set_source_rgb(
            lnewcolor.red / 2 + dnewcolor.red * midtone / 2,
            lnewcolor.green / 2 + dnewcolor.green * midtone / 2,
            lnewcolor.blue / 2 + dnewcolor.blue * midtone / 2,
        )
        ctx.paint()
    ctx.set_source_surface(imgsurface, 0, 0)
    ctx.paint_with_alpha(0.8)

    # Save a cache for later use. Save 'newcolor' in the frist three pixels
    # to check for theme changes between two instances
    fyle = open(temppng, "wb")
    fyle.write(bytes(colors))
    surface.write_to_png(fyle)
Example #8
0
def newtheme (widget):
    global surface, provider, loldcolor, doldcolor
    
    sc = widget.get_style_context()

    # get colors from theme

    # bg color
    found, bgcol = sc.lookup_color("bg_color")
    if not found:        
        found, bgcol = sc.lookup_color("theme_bg_color")
        if not found:
            # fallback value
            bgcol = Gdk.RGBA(red=0.929412, green=0.929412, blue=0.929412, alpha=1.0)

    # bg selected color
    found, bgsel = sc.lookup_color("theme_selected_bg_color")
    if not found:
        # fallback value
        bgsel = Gdk.RGBA(red=0.290, green=0.565, blue=0.851, alpha=1.0)

    # fg color
    found, fgcol = sc.lookup_color("fg_color")
    if not found:
        found, fgcol = sc.lookup_color("theme_fg_color")
        if not found:            
            fgcol= Gdk.RGBA(red=0.180392, green=0.203922, blue=0.211765, alpha=1.000000)

    # base color
    found, basecol = sc.lookup_color("base_color")
    if not found:        
        found, basecol = sc.lookup_color("theme_base_color")
        if not found:
            basecol = Gdk.RGBA(red=0.929412, green=0.929412, blue=0.929412, alpha=1.0)

    # text color
    found, textcol = sc.lookup_color("text_color")
    if not found:        
        found, textcol = sc.lookup_color("theme_text_color")
        if not found:
            textcol = Gdk.RGBA(red=0.180392, green=0.203922, blue=0.211765, alpha=1.0)

    def get_col(col, mult):
        r = col.red * mult
        g = col.green * mult
        b = col.blue * mult
        if r > 1.0: r = 1.0
        if g > 1.0: g = 1.0
        if b > 1.0: b = 1.0
        if r==1 and g==1 and b==1:
            return Gdk.RGBA(0.99, 0.99, 0.99, 1.0)
        else:
            return Gdk.RGBA(r, g, b, 1.0)

    # derive other colors
    bgacol   = get_col(bgcol, 0.9)           # bg_active    
    dcol     = get_col(bgcol, 0.7)           # dark
    darksel  = get_col(bgsel, 0.71)          # dark selected
    dpcol    = get_col(bgcol, 0.71)          # dark prelight
    dacol    = get_col(dcol, 0.9)            # dark_active
    lcol     = get_col(bgcol, 1.3)           # light color
    lightsel = get_col(bgsel, 1.3)           # light selected
    fgsel    = Gdk.RGBA(0.99, 0.99, 0.99, 1.0)  # fg selected
    fgpcol   = get_col(fgcol, 1.054)         # fg prelight
    fgacol   = Gdk.RGBA(0.0, 0.0, 0.0, 1.0)  # fg active
    textaacol=Gdk.RGBA(min((basecol.red+textcol.red)/2., 1.0), min((basecol.green+textcol.green)/2., 1.0), min((basecol.blue+textcol.blue)/2., 1.0))   # text_aa

    # return hex string #rrggbb
    def hexcol(color):
        return "#%02X%02X%02X" % (int(color.red * 255), int(color.green * 255), int(color.blue * 255))

    data = "@define-color p_bg_color "          + hexcol(bgcol) + ";" \
           "@define-color p_bg_prelight "       + hexcol(bgcol) + ";" \
           "@define-color p_bg_active "         + hexcol(bgacol) + ";" \
           "@define-color p_bg_selected "       + hexcol(bgsel) + ";" \
           "@define-color p_bg_insensitive "    + hexcol(bgcol) + ";" \
           "@define-color p_base_color "        + hexcol(basecol) + ";" \
           "@define-color p_dark_color "        + hexcol(dcol) + ";" \
           "@define-color p_dark_prelight "     + hexcol(dpcol) + ";" \
           "@define-color p_dark_active "       + hexcol(dacol) + ";" \
           "@define-color p_dark_selected "     + hexcol(darksel) + ";" \
           "@define-color p_text_aa "           + hexcol(textaacol) + ";" \
           "@define-color p_light_color "       + hexcol(lcol) + ";" \
           "@define-color p_light_selected "    + hexcol(lightsel) + ";" \
           "@define-color p_fg_color "          + hexcol(fgcol) + ";" \
           "@define-color p_fg_prelight "       + hexcol(fgpcol) + ";" \
           "@define-color p_fg_selected "       + hexcol(fgsel) + ";" \
           "@define-color p_fg_active "         + hexcol(fgacol) + ";"

    if provider is not None:        
        sc.remove_provider_for_screen(Gdk.Screen.get_default(), provider)

    provider = Gtk.CssProvider.new()
    provider.load_from_data(data.encode())
    sc.add_provider_for_screen(Gdk.Screen.get_default(), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    lnewcolor = bgcol
    dnewcolor = dcol

    # check if changed
    if loldcolor:
        if lnewcolor.red   == loldcolor.red and \
           lnewcolor.green == loldcolor.green and \
           lnewcolor.blue  == loldcolor.blue and \
           dnewcolor.red   == doldcolor.red and \
           dnewcolor.green == doldcolor.green and \
           dnewcolor.blue  == doldcolor.blue:
            return

    loldcolor = lnewcolor
    doldcolor = dnewcolor

    # global colors have been set up
    # now set colors on startup panel
    bool1, lnewcolor = sc.lookup_color("p_bg_color")    
    bool1, dnewcolor = sc.lookup_color("p_dark_color")
  
    colors = [
        int(lnewcolor.red * 255), int(lnewcolor.green * 255), int(lnewcolor.blue * 255),
        int(dnewcolor.red * 255), int(dnewcolor.green * 255), int(dnewcolor.blue * 255)
    ]

    # Check if a cache has been saved
    temppng = addUserCachePrefix("temp.png")
    if path.isfile(temppng):
        f = open(temppng, "rb")
        # Check if the cache was made while using the same theme
        if list(f.read(6)) == colors:
            surface = cairo.ImageSurface.create_from_png(f)
            return
    
    # Get mostly transparant shadowy image
    imgsurface = cairo.ImageSurface.create_from_png(CLEARPATH)
    AVGALPHA = 108/255.
    
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24,
            imgsurface.get_width(), imgsurface.get_height())
    ctx = cairo.Context (surface)
    if lnewcolor.blue*65535 - dnewcolor.blue*65535 > 0:
        a = dnewcolor.red*65535/(3*(lnewcolor.blue*65535 - dnewcolor.blue*65535)*(1-AVGALPHA))
        ctx.set_source_rgb(
            lnewcolor.red/2  + dnewcolor.red*a/2,
            lnewcolor.green/2 + dnewcolor.green*a/2,
            lnewcolor.blue/2  + dnewcolor.blue*a/2)
        ctx.paint()
    ctx.set_source_surface(imgsurface, 0, 0)
    ctx.paint_with_alpha(.8)
    
    # Save a cache for later use. Save 'newcolor' in the frist three pixels
    # to check for theme changes between two instances
    f = open(temppng, "wb")
    f.write(bytes(colors))
    surface.write_to_png(f)