Generate colors.xml for dark themes with the technology that supports Force Dark

TD;DL --Android # 2 This is the article on the 15th day of the Advent calendar. --It can be generated by executing the script in the repository below. https://github.com/sdsd08013/dark_color_gen

--Since it strongly depends on the color specification method of layout, basically this alone will not be enough for dark themes.

Introduction

One of the features introduced from Android 10 is Force Dark. The item called Force Dark Override in the developer options corresponds to this, and it is a technology that applies a dark theme with relatively high quality when an existing application is started with this option turned on. This article uses the technology behind Force Dark to generate colors.xml for dark themes from existing colors.xml.

Technology that supports Force Dark

Lab color space

The Lab color space is a color space defined based on the CIE1931 color space and is represented by the dimension L indicating lightness and the complementary colors a * b. Compared to the RGB color space that is familiar to us during development, dimension L is a characteristic parameter, which is designed to be close to the brightness of human vision. [1]

Skia A graphic library for 2D rendering that has been adopted by chromium and firefox, which was developed by Skia inc acquired by Google. It was used by default for rendering until Android 3 Honyecomb, but for performance reasons the rendering process was replaced by Hwui, which will be described later. Skia-defined structures are still used for rendering structures.

Hwui Probably an abbreviation for Hardware UI. A graphics library added as a hardware-accelerated option in Android 3 Honyecomb and has become the default since IceCream Sandwitch. It was introduced to express graphics that Skia cannot express due to the high resolution of Android devices and the demand for rich animation in applications.

Realization of Force Dark on Android [3]

On Android, the following steps are taken to convert to a dark theme and perform graphic rendering. Since it will be long to explain each line, it will be omitted here.

-> updateForceDarkMode

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/ViewRootImpl.java#4449

-> setForceDark https://android.googlesource.com/platform/frameworks/base/+/master/graphics/java/android/graphics/HardwareRenderer.java#512

-> applyColorTransform https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/CanvasTransform.cpp#68

-> transformColor https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/CanvasTransform.cpp#57

-> makeDark https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/CanvasTransform.cpp#46

This code is the substance of Force Dark in the process of converting non-dark theme RGB colors to dark theme RGB colors. Convert the RGB color space to the Lab color space, reduce the brightness if necessary, and convert it to the RGB color space again.

static SkColor makeDark(SkColor color) {
    Lab lab = sRGBToLab(color);
    float invertedL = std::min(110 - lab.L, 100.0f);
    if (invertedL < lab.L) {
        lab.L = invertedL;
        return LabToSRGB(lab, SkColorGetA(color));
    } else {
        return color;
    }
}

Generate colors.xml for dark theme

For Skia, it is good because the shared library can be generated relatively easily [2], but for Hwui, the source code of android is too huge and it seems difficult to compile, so extract only the corresponding file and rewrite the header file Corresponding.

macOS Mojava
Version 10.14.3
clang++ --std=c++14 \
    -I ~/skia/include/core \
    -I ~/skia/include/config \
    -I ~/skia/include/utils \
    -I ~/skia/include/gpu \
    -I ~/skia \
    -I transform.h \
    -I Color.h \
    -I ColorSpace.h \
    -L ~/skia/out/Static \
    -lskia -lz transform.cpp Color.cpp ColorSpace.cpp main.cpp

The actual code is below https://github.com/sdsd08013/dark_color_gen

result

The following is the result of generating and building colors.xml for the dark theme from colors.xml of the official application of droidkaigi2018. Although it will be a color like that, it seems that the degree of perfection is low with this alone, and even compared to the actual Android Force Dark, Force Dark that can generate and render colors for dark themes in real time The dark theme is more complete.

refs [1] wiki Lab color space [2] skia official [3]android Git repositories Learning about the Android graphics subsystem

Recommended Posts

Generate colors.xml for dark themes with the technology that supports Force Dark
Plan optimization with AI that understands the reason for the result
Generate colors.xml for dark themes with the technology that supports Force Dark
Plan optimization with AI that understands the reason for the result
[First Java] Make something that works with Intellij for the time being