<p>I am using the awesome Powerlevel9k theme for my Zsh.</p>

<p>I defined a custom kubecontext element to show my kubernetes cluster (context) and namespace (see code below).</p>

<p>While I conditionally set the foreground color through the color variable I would like to set the background color instead to be able to better see when I work on the production cluster.
Is that somehow possible with Powerlevel9k? All I could find is that I can set the background color of the prompt element statically with <code>POWERLEVEL9K_CUSTOM_KUBECONTEXT_BACKGROUND='075'</code></p>

<pre><code># Kubernetes Current Context/Namespace
custom_prompt_kubecontext() {
  local kubectl_version="$(kubectl version --client 2&gt;/dev/null)"

  if [[ -n "$kubectl_version" ]]; then
    # Get the current Kuberenetes context
    local cur_ctx=$(kubectl config view -o=jsonpath='{.current-context}')
    cur_namespace="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")"
    # If the namespace comes back empty set it default.
    if [[ -z "${cur_namespace}" ]]; then
      cur_namespace="default"
    fi

    local k8s_final_text="$cur_ctx/$cur_namespace"

    local color='%F{black}'
    [[ $cur_ctx == "prod" ]] &amp;&amp; color='%F{196}'
    echo -n "%{$color%}\U2388  $k8s_final_text%{%f%}" # \U2388 is Kubernetes Icon

    #"$1_prompt_segment" "$0" "$2" "magenta" "black" "$k8s_final_text" "KUBERNETES_ICON"
  fi
}

POWERLEVEL9K_CUSTOM_KUBECONTEXT="custom_prompt_kubecontext"

# Powerlevel9k configuration
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir vcs custom_kubecontext)
</code></pre>

<p>Here is a screenshot of the current setup in action:</p>

<p><a href="https://i.stack.imgur.com/2cAFI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2cAFI.png" alt="screenshot showing the different kubecontext prompt colors depending on the current kubecontext"></a></p>

Hedge
<p>Disclaimer: I'm the author of powerlevel10k.</p>

<p>No, this is not possible in powerlevel9k. It is, however, possible in powerlevel10k. Powerlevel10k is backward compatible with powerlevel9k configuration, meaning that you won't have to change any <code>POWERLEVEL9K</code> parameters if you decide to switch.</p>

<p>Powerlevel10k has several advantages over its predecessor:</p>

<ol>
<li>It's over 10 times faster.</li>
<li>It has a builtin configuration wizard. Type <code>p10k configure</code> to access it.</li>
<li>It has many new features. One of them is relevant to you. The builtin <code>kubecontext</code> supports <em>context classes</em> that allow you to style this prompt segment differently depending on which kubernetes context is currently active. Here's the excerpt from the configuration that <code>p10k configure</code> generates:</li>
</ol>

<pre><code># Kubernetes context classes for the purpose of using different colors, icons and expansions with
# different contexts.
#
# POWERLEVEL9K_KUBECONTEXT_CLASSES is an array with even number of elements. The first element
# in each pair defines a pattern against which the current kubernetes context gets matched.
# More specifically, it's P9K_CONTENT prior to the application of context expansion (see below)
# that gets matched. If you unset all POWERLEVEL9K_KUBECONTEXT_*CONTENT_EXPANSION parameters,
# you'll see this value in your prompt. The second element of each pair in
# POWERLEVEL9K_KUBECONTEXT_CLASSES defines the context class. Patterns are tried in order. The
# first match wins.
#
# For example, given these settings:
#
#   typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
#     '*prod*'  PROD
#     '*test*'  TEST
#     '*'       DEFAULT)
#
# If your current kubernetes context is "deathray-testing/default", its class is TEST
# because "deathray-testing/default" doesn't match the pattern '*prod*' but does match '*test*'.
#
# You can define different colors, icons and content expansions for different classes:
#
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_FOREGROUND=0
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_BACKGROUND=2
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_VISUAL_IDENTIFIER_EXPANSION='⭐'
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_CONTENT_EXPANSION='&gt; ${P9K_CONTENT} &lt;'
typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
    # '*prod*'  PROD    # These values are examples that are unlikely
    # '*test*'  TEST    # to match your needs. Customize them as needed.
    '*'       DEFAULT)
typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_FOREGROUND=7
typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_BACKGROUND=5
typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_VISUAL_IDENTIFIER_EXPANSION='⎈'
</code></pre>

<p>You can also customize the text content of <code>kubecontext</code>. You'll find more info in <code>~/.p10k.zsh</code> once you run <code>p10k configure</code>. Oh, and <code>kubecontext</code> is about 1000 times faster in powerlevel10k.</p>
