> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/viewer/themes.md).

# Color modes

Learn how to switch between Day and Night themes in Android AppCompat using the DayNight theme. Adjust color modes for improved reading at night with PDFViewCtrl's special color modes. The Apryse Andr

There are a few themes which can be adjusted to change the output color mode.

{% tabs %}
{% tab title="Night" %}

## Day and night themes in Android

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-93e01e121b89132125d6d26eb6d773a0d396181c%2Fead9783185a817347c896967ee91c12ef8d8c82a-2200x2157.png?alt=media)

AppCompat has a new theme called `DayNight` since [Support Lib 23.2.0](https://android-developers.googleblog.com/2016/02/android-support-library-232.html). So, if your activity's theme extends from one of the DayNight variants then you can switch between dark theme and light theme in your activity as follows.

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
int mode = isNight ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
getDelegate().setLocalNightMode(mode);
recreate();
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val mode = if (isNight) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO
delegate.setLocalNightMode(mode)
recreate()
```

{% endcode %}
{% endtab %}
{% endtabs %}

As you can see, to take any effect for changing night mode the activity should be re-created.

For example, in our [`DocumentActivity`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/controls/DocumentActivity.html), we change the device UI night mode when the PDFViewCtrl's color mode is updated:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
@Override
public boolean onColorModeUpdated() {
    boolean isDarkMode = PdfViewCtrlSettingsManager.isDarkMode(this);
        if (isDeviceNightMode() != isDarkMode) {
            getDelegate().setLocalNightMode(isDarkMode
                ? AppCompatDelegate.MODE_NIGHT_YES
                : AppCompatDelegate.MODE_NIGHT_NO);
        recreate();
        return true;
    }
    return false;
}

public boolean isDeviceNightMode() {
    int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK;
    return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
fun onColorModeUpdated(): Boolean {
    val isDarkMode = PdfViewCtrlSettingsManager.isDarkMode(this)
    if (isDeviceNightMode() != isDarkMode) {
        delegate.setLocalNightMode(if (isDarkMode)
            AppCompatDelegate.MODE_NIGHT_YES
        else
            AppCompatDelegate.MODE_NIGHT_NO)
        recreate()
        return true
    }
    return false
}

fun isDeviceNightMode(): Boolean {
    val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
    return currentNightMode == Configuration.UI_MODE_NIGHT_YES
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

You can also have different styles for device day/night modes by creating new resources for night. This involves adding a new directory to the resources folder, using the night qualifier, to re-define the resources. In the following example, primary colors and accent color are different in day and night mode:

*res/values/styles.xml*

{% code lineNumbers="true" %}

```java
<style name="PDFTronAppTheme" parent="PDFTronAppThemeBase"> <item name="colorPrimary">@color/color_primary_day</item> <item name="colorPrimaryDark">@color/color_primary_dark_day</item> <item name="colorAccent">@color/color_accent_day</item> </style>
```

{% endcode %}

*res/values-night/styles.xml*

{% code lineNumbers="true" %}

```xml
<style name="PDFTronAppTheme" parent="PDFTronAppThemeBase"> <item name="colorPrimary">@color/color_primary_night</item> <item name="colorPrimaryDark">@color/color_primary_dark_night</item> <item name="colorAccent">@color/color_accent_night</item> </style>
```

{% endcode %}
{% endtab %}

{% tab title="Custom" %}

## Page color mode

[`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) has three special color modes: night mode, inverted color mode, and custom color mode. In night mode, colors are adjusted to improve reading at night, in inverted color mode, all colors are inverted, and in custom color mode, you can set a custom color for text and the background color.

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-343375628c2dff8eebaa3c1a94bea5d0a9ff71ae%2F66f05a343d0341455b509a9282075128a28bd48a-1430x544.png?alt=media)

Color modes from left to right: normal color mode, inverted color mode, night mode and custom color mode.

## Set the color mode

To set the color mode:

1. Find the constant value of the color mode

| Color mode          | Constant value                                                                                                                                                   |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Normal color mode   | [`PDFRasterizer.e_postprocess_none`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFRasterizer.html#e_postprocess_none)                 |
| Night mode          | [`PDFRasterizer.e_postprocess_night_mode`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFRasterizer.html#e_postprocess_night_mode)     |
| Inverted color mode | [`PDFRasterizer.e_postprocess_invert`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFRasterizer.html#e_postprocess_invert)             |
| Custom color mode   | [`PDFRasterizer.e_postprocess_gradient_map`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFRasterizer.html#e_postprocess_gradient_map) |

1. Call [`setColorPostProcessMode(int)`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html#setColorPostProcessMode\(int\)).
2. Optionally, set the `PDFViewCtrl` background color for the best visual result. You can set background color by calling \[`setClientBackgroundColor(int, int, int, boolean)`]\(<https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html#setClientBackgroundColor(int>, int, int, boolean)).For example, set the background color to black for night mode:

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-e2fd299ceaf0f87ad2aecf8f82269abbfed10f62%2F11a67ce052a7a6199ddd8c12ea5f1144a758c901-392x292.png?alt=media)

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
mPdfViewCtrl.setClientBackgroundColor(0, 0, 0, false);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
mPdfViewCtrl.setClientBackgroundColor(0, 0, 0, false)
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. If you are using `postprocess_gradient_map`, call \[`PDFViewCtrl.setColorPostProcessColors(int, int)`]\(<https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html#setColorPostProcessColors(int>, int)) to set the white and black points. For example, for a sepia effect, try:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
int darkBrown = Color.rgb(61, 38, 10);
int lightBrown = Color.rgb(245, 224, 202);
mPdfViewCtrl.setColorPostProcessColors(lightBrown, darkBrown);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val darkBrown = Color.rgb(61, 38, 10)
val lightBrown = Color.rgb(245, 224, 202)
mPdfViewCtrl.setColorPostProcessColors(lightBrown, darkBrown)
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Update `PDFViewCtrl` to redraw the contents:Sample result when using `postprocess_gradient_map` and a light/dark brown for the white/black color: The example below shows all of this functionality being used to set up a custom color mode for `PDFViewCtrl`:Result:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
mPdfViewCtrl.update(true);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
mPdfViewCtrl.update(true)
```

{% endcode %}
{% endtab %}
{% endtabs %}

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-147581910216d35f3a25250590fcd193fc50dc02%2F763253aeabe683dda72a42172754b12942b3ec44-948x1228.png?alt=media)

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
private PDFViewCtrl mPdfViewCtrl;
// ...
// Sets background color to dark gray, as well as text color to red.
private void setCustomColorMode(@ColorInt int backgroundColor, @ColorInt int textColor) throws PDFNetException {
    // Sets color process mode
    mPdfViewCtrl.setColorPostProcessMode(PDFRasterizer.e_postprocess_gradient_map);
    // Sets client area color to backgroundColor
    mPdfViewCtrl.setClientBackgroundColor(Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor), false);
    // Sets background color and text color
    mPdfViewCtrl.setColorPostProcessColors(backgroundColor, textColor);
    // Updates mPdfViewCtrl
    mPdfViewCtrl.update(true);
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
private var mPdfViewCtrl: PDFViewCtrl? = null
// ...
// Sets background color to dark gray, as well as text color to red.
@Throws(PDFNetException::class)
private fun setCustomColorMode(@ColorInt backgroundColor: Int, @ColorInt textColor: Int) {
    // Sets color process mode
    mPdfViewCtrl!!.colorPostProcessMode = PDFRasterizer.e_postprocess_gradient_map
    // Sets client area color to backgroundColor
    mPdfViewCtrl!!.setClientBackgroundColor(Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor), false)
    // Sets background color and text color
    mPdfViewCtrl!!.setColorPostProcessColors(backgroundColor, textColor)
    // Updates mPdfViewCtrl
    mPdfViewCtrl!!.update(true)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-fa45b1967c83b75c75da8974229762c5db6c9005%2Fde9f862b29e75052c9b873280d3a3fa32321b8d8-351x622.png?alt=media)

**Alternatively, you can also set custom color mode by adding a gradient map image as follows:**

1. Add a gradient map `.png` image to your project `res/raw` folder.For example, add the following image to `res/raw` folder and name it `custom_mode_filter.png`:

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-3c0ca5c6f066e2260505de331c00ea915f61927e%2Ff3c9eaa3ef590b6ec62c35026e0d1c373a7a8279-257x49.png?alt=media)

{% hint style="info" %}
To learn more about res/raw resource directory, see [Providing Resources](https://developer.android.com/guide/topics/resources/providing-resources.html)
{% endhint %}

1. Set the gradient image to a new [`MappedFile`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/filters/MappedFile.html):

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
InputStream is = null;
OutputStream os = null;
MappedFile mappedFile = null;
try {
    File filterFile = new File(activity.getCacheDir(), "cachedCustomColorFilter.png");
    if (!filterFile.exists() || !filterFile.isFile()) {
        is = getResources().openRawResource(R.raw.custom_mode_filter);
        os = new FileOutputStream(filterFile);
        IOUtils.copy(is, os);
    }
    mappedFile = new MappedFile(filterFile.getAbsolutePath());
} catch (Exception e) {
    e.printStackTrace();
} finally {
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(os);
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
var inputStream: InputStream? = null
var outputStream: OutputStream? = null
var mappedFile: MappedFile? = null
try {
    val filterFile = File(activity.getCacheDir(), "cachedCustomColorFilter.png")
    if (!filterFile.exists() || !filterFile.isFile) {
        inputStream = resources.openRawResource(R.raw.custom_mode_filter)
        outputStream = FileOutputStream(filterFile)
        IOUtils.copy(inputStream, outputStream)
    }
    mappedFile = MappedFile(filterFile.absolutePath)
} catch (e: Exception) {
    e.printStackTrace()
} finally {
    IOUtils.closeQuietly(inputStream)
    IOUtils.closeQuietly(outputStream)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Set the `mappedFile` created above to `PDFViewCtrl` by calling [`PDFViewCtrl.setColorPostProcessMapFile(Filter)`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html#setColorPostProcessMapFile\(com.pdftron.filters.Filter\)).Where `pdfViewCtrl` is an instance of `PDFViewCtrl`.

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
if (mappedFile != null) {
    try {
        pdfViewCtrl.setColorPostProcessMapFile(mappedFile);
    } catch (PDFNetException e) {
        e.printStackTrace();
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
if (mappedFile != null) {
    try {
        pdfViewCtrl!!.setColorPostProcessMapFile(mappedFile)
    } catch (e: PDFNetException) {
        e.printStackTrace()
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Set the color process mode `PDFRasterizer.e_postprocess_gradient_map` to `pdfViewCtrl` by calling `pdfViewCtrl.setColorPostProcessMode(int)`. Then update `pdfViewCtrl`:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
try {
    pdfViewCtrl.setColorPostProcessMode(PDFRasterizer.e_postprocess_gradient_map);
    // Sets client area background color to rgb(138, 138, 138)
    pdfViewCtrl.setClientBackgroundColor(138, 138, 138, false);
    // Updates pdfViewCtrl
    pdfViewCtrl.update(true);
} catch (PDFNetException e) {
    e.printStackTrace();
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
try {
  pdfViewCtrl.colorPostProcessMode = PDFRasterizer.e_postprocess_gradient_map
  // Sets client area background color to rgb(138, 138, 138)
  pdfViewCtrl.setClientBackgroundColor(138, 138, 138, false)
  // Updates pdfViewCtrl
  pdfViewCtrl.update(true)
} catch (e: PDFNetException) {
  e.printStackTrace()
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

Result:

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-a40e05f286743d9f65c50fd210301fcc233b6e92%2Fdc240a718e3bfc4be97b25926b58067208ffab5c-350x572.png?alt=media)
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/viewer/themes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
