Skip to content

Commit

Permalink
Support for hexadecimal and HTML colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
dario2994 committed Jun 20, 2022
1 parent c55ff5a commit 5f2f181
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ It must be a valid `yaml` file containing the following top-level keys:
- `name` (mandatory): Short-name, in polygon, of the problem. This is used as identifier of the problem (denoted above as `problem_name`).
- `polygon_id`: The problem id in polygon. Can be found in the right-side menu after opening the problem in polygon. It is necessary to download the polygon package.
- `label`: The label -- usually an uppercase letter -- used to identify the problem in the DOMjudge scoreboard. Problems will be appear in DOMjudge sorted according to this label.
- `color`: Color of the problem in DOMjudge.
- `color`: Color of the problem in DOMjudge. It can be either the name of an HTML color (see the list https://htmlcolorcodes.com/color-names/) or the hexadecimal representation of a color (e.g., '#FF12AB'). If the hexadecimal representation is used, it must start with # and it must be enclosed in single or double quotes.
- `author`: The author of the problem. Used in the pdf of the solutions.
- `preparation`: The person who prepared the problems. Used in the pdf of the solutions.
- `override_time_limit`: Value (in seconds) of the time limit of the problem in DOMjudge. If this is present the value set in polygon is ignored.
Expand Down
4 changes: 2 additions & 2 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ domjudge: # Credentials for the DOMjudge instance APIs. Must be an admin user.
problems:
- name: gianni
label: A
color: CornflowerBlue
color: CornflowerBlue # HTML color: https://htmlcolorcodes.com/color-names/
override_memory_limit: 2048 # MiB
# To find the problem id in polygon:
# 1. Open the problem in polygon
Expand All @@ -28,7 +28,7 @@ problems:
preparation: Federico Glaudo
- name: drone-photo
label: B
color: PineGreen
color: '#FFFF00' # Also hexadecimal colors are valid.
override_time_limit: 3 # seconds
polygon_id: ???
author: Andrea Ciprietti
Expand Down
2 changes: 1 addition & 1 deletion p2d/generate_domjudge_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def generate_domjudge_package(problem, domjudge, tex_dir, params):
'short-name = %s' % problem['name'],
'name = %s' % problem['title'].replace("'", "`"),
'timelimit = %s' % problem['timelimit'],
'color = %s' % problem['color']
'color = #%s' % problem['color']
]
with open(ini_file, 'w', encoding='utf-8') as f:
f.writelines(map(lambda s: s + '\n', ini_content))
Expand Down
35 changes: 27 additions & 8 deletions p2d/p2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import logging
import os
import pathlib
import re
import shutil
import string
import sys
import tempfile
import webcolors
import yaml
import zipfile
from argparse import ArgumentParser
Expand All @@ -20,6 +22,28 @@
RESOURCES_PATH = os.path.join(
os.path.split(os.path.realpath(__file__))[0], 'resources')

# Get a color in one of the following formats:
# - #FF11AB (hexadecimal, upper case, with #)
# - #ff11ab (hexadecimal, lower case, with #)
# - PapayaWhip (HTML color https://htmlcolorcodes.com/color-names/, camel case)
# - papayawhip (HTML color https://htmlcolorcodes.com/color-names/, lower case)
# and converts it to its standard 6-digit hexadecimal representation (e.g., FF11AB).
def convert_to_hex(color):
error_message = 'The color \'%s\' specified in config.yaml is not a valid html color (see https://htmlcolorcodes.com/color-names/) or a valid hexadecimal color (e.g., #ABC123). ' % color

if color[0] == '#':
color = color[1:]
if not re.fullmatch(r'[A-Fa-f0-9]{6}', color):
logging.error(error_message)
exit(1)
else:
try:
color = webcolors.name_to_hex(color)[1:]
except ValueError:
logging.error(error_message)
exit(1)

return color.upper()

def manage_download(config, polygon_dir, problem):
if 'polygon_id' not in problem:
Expand Down Expand Up @@ -108,7 +132,7 @@ def manage_convert(config, polygon_dir, domjudge_dir, tex_dir, problem):
logging.warning('The keys %s are not set in config.yaml for this problem.' % missing_keys)

problem_package['label'] = problem.get('label', '?')
problem_package['color'] = problem.get('color', 'Black')
problem_package['color'] = convert_to_hex(problem.get('color', 'Black'))

if 'override_time_limit' in problem:
problem_package['timelimit'] = problem['override_time_limit']
Expand Down Expand Up @@ -384,8 +408,8 @@ def save_config_yaml():

print()
logging.info('Successfully generated \'%s\' and \'%s\'.' %
(os.path.join(contest_dir, 'tex', 'problemset.tex'),
os.path.join(contest_dir, 'tex', 'solutions.tex')))
(os.path.join(contest_dir, 'tex', 'problemset.pdf'),
os.path.join(contest_dir, 'tex', 'solutions.pdf')))

# On error tracing and logging:
#
Expand All @@ -409,11 +433,6 @@ def main():


# TODO: Everything should be tested appropriately.
# TODO: The colors belong to what list?
# Must work with latex and must also work with Utils::convertToHex
# github.com/DOMjudge/domjudge/blob/main/webapp/src/Utils/Utils.php#L268
# The easiest and most consistent solution, but not user-friendly, is to
# require the colors to be hexadecimal (and add support for them in latex).
# TODO: --problem should be considered also for --clear-dir and --clear-domjudge-ids.
# TODO: Check that config.yaml does not contain unrelated keys.
# TODO: Add the support for interactive problems.
5 changes: 3 additions & 2 deletions p2d/resources/document_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
\newcommand\balloon{%
\if \showballoon 1
\begin{tikzpicture}[scale=0.5, overlay, shift={(34.5, 0.5)}]
\shade[ball color = \problemcolor] ellipse (1.75 and 2);
\shade[ball color = \problemcolor] (-.1,-2) -- (-.3,-2.2) -- (.3,-2.2) -- (.1,-2) -- cycle;
\shade[ball color = \problemcolorname] ellipse (1.75 and 2);
\shade[ball color = \problemcolorname] (-.1,-2) -- (-.3,-2.2) -- (.3,-2.2) -- (.1,-2) -- cycle;
\path (0, -2.2) edge [out=250, in=120] (0.3, -4);
\path (0.3, -4) edge [out=-60, in=60] (0, -6);
\end{tikzpicture}
Expand Down Expand Up @@ -258,6 +258,7 @@ \subsection*{Explanation of sample \arabic{samplescnt}.}

\newcommand\problemlabel{undefined}
\newcommand\problemcolor{undefined}
\newcommand\problemcolorname{undefined}
\newcommand\problemtitle{undefined}
\newcommand\timelimit{undefined}
\newcommand\memorylimit{undefined}
Expand Down
3 changes: 3 additions & 0 deletions p2d/resources/solution_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
\renewcommand\problemauthor{??AUTHOR??}
\renewcommand\problempreparation{??PREPARATION??}

\renewcommand\problemcolorname{problemcolorname\problemlabel}
\definecolor\problemcolorname{HTML}{\problemcolor}

\pagestyle{solution}

\solutionheader
Expand Down
3 changes: 3 additions & 0 deletions p2d/resources/statement_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
\renewcommand\timelimit{??TIMELIMIT??}
\renewcommand\memorylimit{??MEMORYLIMIT??}

\renewcommand\problemcolorname{problemcolorname\problemlabel}
\definecolor\problemcolorname{HTML}{\problemcolor}

\pagestyle{problem}

\problemheader
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_version():
platforms='any',
install_requires=[
'pyyaml >= 5.3',
'requests >= 2.26'
'requests >= 2.26',
'webcolors >= 1.0'
]
)

0 comments on commit 5f2f181

Please sign in to comment.