Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stylix: add darker and even-darker polarities #694

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ If you only set a wallpaper, Stylix will use a
to create a color scheme. The quality of these schemes can vary, but more
colorful images tend to have better results.

You can force a light or dark scheme using the polarity option:
You can force a light or dark scheme using the `polarity.force` option:

```nix
{
stylix.polarity = "dark";
stylix.force.polarity = "dark";
}
```

Expand Down
18 changes: 10 additions & 8 deletions modules/gnome/hm.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{ pkgs, config, lib, ... }:

let
{
pkgs,
config,
lib,
...
}: let
inherit (config.stylix.fonts) sansSerif serif monospace;
fontSize = toString config.stylix.fonts.sizes.applications;
documentFontSize = toString (config.stylix.fonts.sizes.applications - 1);

in {
options.stylix.targets.gnome.enable =
config.lib.stylix.mkEnableTarget "GNOME" true;
Expand Down Expand Up @@ -35,7 +37,7 @@ in {
# settings tile is removed. The value is still used by Epiphany to
# request dark mode for websites which support it.
color-scheme =
if config.stylix.polarity == "dark"
if config.stylix.polarity.force == "dark"
then "prefer-dark"
else "default";

Expand All @@ -50,11 +52,11 @@ in {
};

xdg.dataFile."themes/Stylix/gnome-shell/gnome-shell.css" = {
source =
let theme = pkgs.callPackage ./theme.nix {
source = let
theme = pkgs.callPackage ./theme.nix {
inherit (config.lib.stylix) colors templates;
};
in "${theme}/share/gnome-shell/gnome-shell.css";
in "${theme}/share/gnome-shell/gnome-shell.css";
onChange = ''
if [[ -x "$(command -v gnome-extensions)" ]]; then
gnome-extensions disable [email protected]
Expand Down
10 changes: 8 additions & 2 deletions modules/kubecolor/hm.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{ config, lib, ... }:
{
config,
lib,
...
}: {
options.stylix.targets.kubecolor.enable = config.lib.stylix.mkEnableTarget "kubecolor" true;

config = lib.mkIf config.stylix.targets.kubecolor.enable {
programs.kubecolor.settings = {
preset = if config.stylix.polarity == "either" then "" else "${config.stylix.polarity}";
preset =
if config.stylix.polarity.force == "either"
then ""
else "${config.stylix.polarity.force}";
theme = {
base = {
info = "fg=${config.lib.stylix.colors.withHashtag.base05-hex}";
Expand Down
15 changes: 8 additions & 7 deletions modules/qutebrowser/hm.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{ config, lib, ... }:

{
config,
lib,
...
}:
with config.lib.stylix.colors.withHashtag;
with config.stylix.fonts;

let
with config.stylix.fonts; let
background = base00;
secondary-background = base01;
selection-background = base03;
Expand Down Expand Up @@ -247,13 +248,13 @@ in {
};

webpage = let
isDark = config.stylix.polarity == "dark";
isDark = config.stylix.polarity.force == "dark";
in {
darkmode.enabled = lib.mkIf isDark (lib.mkDefault true);

preferred_color_scheme =
lib.mkIf
isDark (lib.mkDefault config.stylix.polarity);
isDark (lib.mkDefault config.stylix.polarity.force);
};
};

Expand Down
23 changes: 16 additions & 7 deletions palette-generator/Stylix/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,39 @@ import System.Environment ( getArgs )
import System.Exit ( die )
import System.Random ( setStdGen, mkStdGen )
import Text.JSON ( encode )
import Text.Read (readMaybe)

-- String to float
stringToDouble :: String -> Double
stringToDouble s = case readMaybe s of
Just x -> x
Nothing -> error "Invalid float value"

-- | Load an image file.
loadImage :: String -- ^ Path to the file
-> IO DynamicImage
loadImage input = either error id <$> readImage input

mainProcess :: (String, String, String) -> IO ()
mainProcess (polarity, input, output) = do
mainProcess :: (String,String,String, String, String) -> IO ()
mainProcess (polarity, primaryScaleDark, primaryScaleLight, input, output) = do
putStrLn $ "Processing " ++ input

-- Random numbers must be deterministic when running inside Nix.
setStdGen $ mkStdGen 0

image <- loadImage input
palette <- evolve (polarity, convertRGB8 image)
palette <- evolve (polarity,stringToDouble primaryScaleDark,stringToDouble primaryScaleLight,convertRGB8 image)
let outputTable = makeOutputTable $ V.map lab2rgb palette

writeFile output $ encode outputTable
putStrLn $ "Saved to " ++ output

parseArguments :: [String] -> Either String (String, String, String)
parseArguments [polarity, input, output] = Right (polarity, input, output)
parseArguments [_, _] = Left "Please specify an output file"
parseArguments [_] = Left "Please specify an image"
parseArguments :: [String] -> Either String (String, String,String,String, String)
parseArguments [polarity, primaryScaleDark, primaryScaleLight,input, output] = Right (polarity,primaryScaleDark, primaryScaleLight, input, output)
parseArguments [_, _,_,_] = Left "Please specify an output file"
parseArguments [_, _,_] = Left "Please specify an image"
parseArguments [_, _] = Left "Please specify a primary scaling factor for the light polarity"
parseArguments [_] = Left "Please specify a primary scaling factor for the dark polarity"
parseArguments [] = Left "Please specify a polarity: either, light or dark"
parseArguments _ = Left "Too many arguments"

Expand Down
56 changes: 50 additions & 6 deletions palette-generator/Stylix/Palette.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Data.Vector ( (//) )
import qualified Data.Vector as V
import System.Random ( randomRIO )

-- Adjust value according to coefficients
adjustValue :: Double -> Double-> Double -> Double -> Double
adjustValue v a b x = minimum [maximum [0.0, v*(a*x+b)], 100.0]

-- | Extract the primary scale from a pallete.
primary :: V.Vector a -> V.Vector a
primary = V.take 8
Expand All @@ -34,17 +38,17 @@ randomFromImage image = do
color = RGB (fromIntegral r) (fromIntegral g) (fromIntegral b)
return $ rgb2lab color

instance Species (String, Image PixelRGB8) (V.Vector LAB) where
generate (_, image) = V.replicateM 16 $ randomFromImage image
instance Species (String,Double,Double, Image PixelRGB8) (V.Vector LAB) where
generate (_,_,_, image) = V.replicateM 16 $ randomFromImage image

crossover _ a b = return $ alternatingZip a b

mutate (_, image) palette = do
mutate (_,_,_, image) palette = do
index <- randomRIO (0, 15)
colour <- randomFromImage image
return $ palette // [(index, colour)]

fitness (polarity, _) palette
fitness (polarity,primaryScaleDark,primaryScaleLight, _) palette
= realToFrac $ accentDifference - (primarySimilarity/10) - scheme
where
-- The primary scale should use similar colours, to an extent.
Expand Down Expand Up @@ -82,11 +86,51 @@ instance Species (String, Image PixelRGB8) (V.Vector LAB) where
The accent colours are slightly darker.
-}
lightScheme
= lightnessError (V.fromList [90, 70, 55, 35, 25, 10, 5, 5]) 40
= lightnessError (V.fromList [
(adjustValue 90.0 0.1 0.0 primaryScaleLight),
(adjustValue 70.0 0.963 0.037 primaryScaleLight),
(adjustValue 55.0 0.913 0.087 primaryScaleLight),
(adjustValue 35.0 0.167 0.883 primaryScaleLight),
(adjustValue 25.0 0.078 0.922 primaryScaleLight),
(adjustValue 10.0 0.056 0.944 primaryScaleLight),
(adjustValue 5.0 0.0 1.0 primaryScaleLight),
(adjustValue 5.0 0.0 1.0 primaryScaleLight)
]) 40

-- 0.1, 0.133,0.178,0.85,0.93,0.95,1.0,1.0 for 0.1 scale
-- f(x) = ax+b for multiplier (light)
-- 1.0, 0.0
-- 0.963, 0.037
-- 0.913, 0.087
-- 0.167, 0.833
-- 0.078, 0.922
-- 0.056, 0.944
-- 0.0, 1.0
-- 0.0, 1.0

{-
For dark themes, the background is dark and the text is bright.
The accent colours are slightly brighter.
-}
darkScheme
= lightnessError (V.fromList [10, 30, 45, 65, 75, 90, 95, 95]) 60
= lightnessError (V.fromList [
(adjustValue 10.0 1.0 0.0 primaryScaleDark),
(adjustValue 30.0 0.963 0.037 primaryScaleDark),
(adjustValue 45.0 0.913 0.087 primaryScaleDark),
(adjustValue 65.0 0.167 0.883 primaryScaleDark),
(adjustValue 75.0 0.078 0.922 primaryScaleDark),
(adjustValue 90.0 0.056 0.944 primaryScaleDark),
(adjustValue 95.0 0.0 1.0 primaryScaleDark),
(adjustValue 95.0 0.0 1.0 primaryScaleDark)
]) 60

-- 0.1, 0.133,0.178,0.85,0.93,0.95,1.0,1.0 for 0.1 scale
-- f(x) = ax+b for multiplier (dark)
-- 1.0, 0.0
-- 0.963, 0.037
-- 0.913, 0.087
-- 0.167, 0.833
-- 0.078, 0.922
-- 0.056, 0.944
-- 0.0, 1.0
-- 0.0, 1.0
44 changes: 24 additions & 20 deletions stylix/hm/icon.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{ config, lib, ... }:

let
cfg = config.stylix.iconTheme;
inherit (config.stylix) polarity;
{
config,
lib,
...
}: let
cfg = config.stylix.iconTheme;
inherit (config.stylix) polarity;
in {
imports = [ ../icon.nix ];
config = lib.mkIf (config.stylix.enable && cfg.enable) {
gtk = {
iconTheme = {
inherit (cfg) package;
name = builtins.head (lib.filter (x: null != x) [
({
inherit (cfg) dark light;
}."${polarity}" or null)
cfg.dark
cfg.light
]);
};
};
};
imports = [../icon.nix];
config = lib.mkIf (config.stylix.enable && cfg.enable) {
gtk = {
iconTheme = {
inherit (cfg) package;
name = builtins.head (lib.filter (x: null != x) [
({
inherit (cfg) dark light;
}
."${polarity.force}"
or null)
cfg.dark
cfg.light
]);
};
};
};
}
78 changes: 44 additions & 34 deletions stylix/home-manager-integration.nix
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
homeManagerModule:
{ lib, config, options, ... }:

let
copyModules = builtins.map
homeManagerModule: {
lib,
config,
options,
...
}: let
copyModules =
builtins.map
(
{ path, condition ? lib.const true }:
{ config, osConfig, ... }:
lib.mkIf (condition config)
(lib.setAttrByPath path (lib.mkDefault (lib.getAttrFromPath path osConfig)))
{
path,
condition ? lib.const true,
}: {
config,
osConfig,
...
}:
lib.mkIf (condition config)
(lib.setAttrByPath path (lib.mkDefault (lib.getAttrFromPath path osConfig)))
)
[
{ path = [ "stylix" "autoEnable" ]; }
{path = ["stylix" "autoEnable"];}
{
path = [ "stylix" "base16Scheme" ];
path = ["stylix" "base16Scheme"];
condition = homeConfig: config.stylix.image == homeConfig.stylix.image;
}
{ path = [ "stylix" "cursor" "name" ]; }
{ path = [ "stylix" "cursor" "package" ]; }
{ path = [ "stylix" "cursor" "size" ]; }
{ path = [ "stylix" "enable" ]; }
{ path = [ "stylix" "fonts" "serif" ]; }
{ path = [ "stylix" "fonts" "sansSerif" ]; }
{ path = [ "stylix" "fonts" "monospace" ]; }
{ path = [ "stylix" "fonts" "emoji" ]; }
{ path = [ "stylix" "fonts" "sizes" "desktop" ]; }
{ path = [ "stylix" "fonts" "sizes" "applications" ]; }
{ path = [ "stylix" "fonts" "sizes" "terminal" ]; }
{ path = [ "stylix" "fonts" "sizes" "popups" ]; }
{ path = [ "stylix" "image" ]; }
{ path = [ "stylix" "imageScalingMode" ]; }
{ path = [ "stylix" "opacity" "desktop" ]; }
{ path = [ "stylix" "opacity" "applications" ]; }
{ path = [ "stylix" "opacity" "terminal" ]; }
{ path = [ "stylix" "opacity" "popups" ]; }
{path = ["stylix" "cursor" "name"];}
{path = ["stylix" "cursor" "package"];}
{path = ["stylix" "cursor" "size"];}
{path = ["stylix" "enable"];}
{path = ["stylix" "fonts" "serif"];}
{path = ["stylix" "fonts" "sansSerif"];}
{path = ["stylix" "fonts" "monospace"];}
{path = ["stylix" "fonts" "emoji"];}
{path = ["stylix" "fonts" "sizes" "desktop"];}
{path = ["stylix" "fonts" "sizes" "applications"];}
{path = ["stylix" "fonts" "sizes" "terminal"];}
{path = ["stylix" "fonts" "sizes" "popups"];}
{path = ["stylix" "image"];}
{path = ["stylix" "imageScalingMode"];}
{path = ["stylix" "opacity" "desktop"];}
{path = ["stylix" "opacity" "applications"];}
{path = ["stylix" "opacity" "terminal"];}
{path = ["stylix" "opacity" "popups"];}
{
path = [ "stylix" "override" ];
path = ["stylix" "override"];
condition = homeConfig: config.stylix.base16Scheme == homeConfig.stylix.base16Scheme;
}
{ path = [ "stylix" "polarity" ]; }
{path = ["stylix" "polarity" "force"];}
{path = ["stylix" "polarity" "primaryScale" "dark"];}
{path = ["stylix" "polarity" "primaryScale" "light"];}
];

in {
options.stylix.homeManagerIntegration = {
followSystem = lib.mkOption {
Expand Down Expand Up @@ -73,7 +83,7 @@ in {
lib.optionalAttrs (options ? home-manager)
(lib.mkIf config.stylix.homeManagerIntegration.autoImport {
home-manager.sharedModules =
[ homeManagerModule ] ++
(lib.optionals config.stylix.homeManagerIntegration.followSystem copyModules);
[homeManagerModule]
++ (lib.optionals config.stylix.homeManagerIntegration.followSystem copyModules);
});
}
Loading
Loading