[Kandinsky] drawChar now takes two colors

Change-Id: I0d99bb5e6c22ad582d82eae643168e6c1118f434
This commit is contained in:
Romain Goyet
2016-09-15 16:34:01 +02:00
parent 8f9d9d0b0b
commit 3e62ead069
13 changed files with 55 additions and 18 deletions

View File

@@ -30,7 +30,7 @@ void Probability::LawCell::drawRect(KDContext * ctx, KDRect rect) const {
//KDColor background = m_even ? KDColor(0xEEEEEE) : KDColor(0x777777);
//ctx->fillRect(rect, background);
//ctx->drawString(m_message, KDPoint(margin+10, 5), textColor, backgroundColor);
ctx->drawString(m_message, KDPoint(margin+10, 5), textColor, backgroundColor);
}
void Probability::LawCell::setMessage(const char * message) {

View File

@@ -16,7 +16,7 @@ void StackView::setName(const char * name) {
void StackView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(rect, KDColor(0xFFCD50));
ctx->drawString(m_name, {0,0}, true);
ctx->drawString(m_name, KDPointZero);
}
#if ESCHER_VIEW_LOGGING

View File

@@ -21,7 +21,9 @@ void TabViewCell::setActive(bool active) {
}
void TabViewCell::drawRect(KDContext * ctx, KDRect rect) const {
ctx->drawString(m_name, {0,0}, m_active);
KDColor text = m_active ? KDColorBlack : KDColorWhite;
KDColor background = m_active ? KDColorWhite : KDColorBlack;
ctx->drawString(m_name, KDPointZero, text, background);
}
#if ESCHER_VIEW_LOGGING

View File

@@ -13,7 +13,7 @@ TextField::TextField(Responder * parentResponder, char * textBuffer, size_t text
void TextField::drawRect(KDContext * ctx, KDRect rect) const {
m_textBuffer[m_currentTextLength] = 0;
ctx->drawString(m_textBuffer, KDPointZero, 0);
ctx->drawString(m_textBuffer, KDPointZero);
}
#if ESCHER_VIEW_LOGGING

View File

@@ -175,7 +175,7 @@ int main(int argc, char * argv[]) {
fprintf(sourceFile, " {");
for (int x = 0; x < glyph_width; x++) {
pixel_t * pixel = (bitmap_image.pixels + (y+characterY)*bitmap_image.width + (x+characterX));
fprintf(sourceFile, "0x%02x", pixel->green);
fprintf(sourceFile, "0x%02x", 0xFF - pixel->green);
if (x+1 < glyph_width) {
fprintf(sourceFile, ", ");
}

View File

@@ -10,10 +10,12 @@ public:
constexpr KDColor(uint32_t rgb)
: m_value(((rgb&0xF80000)>>8)|((rgb&0x00FC00)>>5)|((rgb&0x0000F8)>>3)) {}
constexpr KDColor(uint8_t r, uint8_t g, uint8_t b)
: m_value((r>>3)<<11 | (g>>2) << 5 | (b>>3)) {}
uint8_t red();
uint8_t green();
uint8_t blue();
KDColor blend(KDColor other, uint8_t alpha);
static KDColor blend(KDColor first, KDColor second, uint8_t alpha);
operator uint16_t() const { return m_value; }
private:
uint16_t m_value;

View File

@@ -14,8 +14,8 @@ public:
KDColor getPixel(KDPoint p);
// Text
void drawChar(char character, KDPoint p, uint8_t inverse);
void drawString(const char * text, KDPoint p, uint8_t inverse);
void drawChar(char character, KDPoint p, KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite);
void drawString(const char * text, KDPoint p, KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite);
// Line. Not anti-aliased.
void drawLine(KDPoint p1, KDPoint p2, KDColor c);

View File

@@ -1,7 +1,20 @@
#include <kandinsky/color.h>
KDColor KDColor::blend(KDColor other, uint8_t alpha) {
return other; // FIXME
KDColor KDColor::blend(KDColor first, KDColor second, uint8_t alpha) {
// We want to do first*alpha + second*(1-alpha)
// First is RRRRR GGGGGG BBBBB
// Second is same
uint8_t oneMinusAlpha = 0xFF-alpha;
uint16_t red = first.red()*alpha + second.red()*oneMinusAlpha;
uint16_t green = first.green()*alpha + second.green()*oneMinusAlpha;
uint16_t blue = first.blue()*alpha + second.blue()*oneMinusAlpha;
return KDColor(red>>8, green>>8, blue>>8);
// Blend White + black, ask for white
// white.red() = 0x1F << 3 = 0xF8
// white.red() * 0xFF = 0xF708, we wanted 0xF800
}
uint8_t KDColor::red() {

View File

@@ -56,7 +56,7 @@ void KDContext::fillRectWithMask(KDRect rect, KDColor color, const uint8_t * mas
for (KDCoordinate i=0; i<absoluteRect.width(); i++) {
KDColor * currentPixelAdress = workingBuffer + i + absoluteRect.width()*j;
const uint8_t * currentMaskAddress = mask + i + rect.width()*j;
*currentPixelAdress = currentPixelAdress->blend(color, *currentMaskAddress);
*currentPixelAdress = KDColor::blend(*currentPixelAdress, color, *currentMaskAddress);
//*currentPixelAdress = KDColorBlend(*currentPixelAdress, color, *currentMaskAddress);
}
}

View File

@@ -3,6 +3,20 @@
KDColor characterBuffer[BITMAP_FONT_CHARACTER_WIDTH*BITMAP_FONT_CHARACTER_HEIGHT];
void KDContext::drawChar(char character, KDPoint p, KDColor textColor, KDColor backgroundColor) {
for (int j=0; j<BITMAP_FONT_CHARACTER_HEIGHT;j++) {
for (int i=0; i<BITMAP_FONT_CHARACTER_WIDTH;i++) {
uint8_t intensity = bitmapFont[character-BITMAP_FONT_FIRST_CHARACTER][j][i];
KDColor color = KDColor::blend(textColor, backgroundColor, intensity);
//characterBuffer[j*BITMAP_FONT_CHARACTER_WIDTH+i] = color;
characterBuffer[j*BITMAP_FONT_CHARACTER_WIDTH+i] = color;
}
}
fillRectWithPixels(KDRect(p, BITMAP_FONT_CHARACTER_WIDTH, BITMAP_FONT_CHARACTER_HEIGHT),
characterBuffer,
characterBuffer);
}
#if 0
void KDContext::drawChar(char character, KDPoint p, uint8_t inverse) {
for (int j=0; j<BITMAP_FONT_CHARACTER_HEIGHT;j++) {
for (int i=0; i<BITMAP_FONT_CHARACTER_WIDTH;i++) {
@@ -17,12 +31,13 @@ void KDContext::drawChar(char character, KDPoint p, uint8_t inverse) {
characterBuffer,
characterBuffer);
}
#endif
void KDContext::drawString(const char * text, KDPoint p, uint8_t inverse) {
void KDContext::drawString(const char * text, KDPoint p, KDColor textColor, KDColor backgroundColor) {
KDPoint position = p;
KDPoint characterSize(BITMAP_FONT_CHARACTER_WIDTH, 0);
while(*text != 0) {
drawChar(*text, position, inverse);
drawChar(*text, position, textColor, backgroundColor);
text++;
position = position.translatedBy(characterSize);
}

View File

@@ -17,3 +17,10 @@ QUIZ_CASE(kandinsky_color_rgb) {
assert(KDColor(0x123456) == 0x11AA);
}
QUIZ_CASE(kandinsky_color_blend) {
KDColor midGray = KDColor(0x7F7F7F);
KDColor res = KDColor::blend(KDColorWhite, KDColorBlack, 0xFF);
assert(res == KDColorWhite);
assert(KDColor::blend(KDColorWhite, KDColorBlack, 0) == KDColorBlack);
assert(KDColor::blend(KDColorWhite, KDColorBlack, 0x7F) == midGray);
}

View File

@@ -2,12 +2,11 @@
#include <stdlib.h>
#include "string_layout.h"
StringLayout::StringLayout(const char * string, size_t length, uint8_t inverse) :
StringLayout::StringLayout(const char * string, size_t length) :
ExpressionLayout() {
assert(string[length] == 0); // Assert NULL-termination
m_string = (char *)malloc(sizeof(char)*(length+1));
memcpy(m_string, string, (length+1));
m_inverse = inverse;
// Height of the font.
m_baseline = KDText::stringSize(" ").height();
}
@@ -21,7 +20,7 @@ ExpressionLayout * StringLayout::child(uint16_t index) {
}
void StringLayout::render(KDContext * ctx, KDPoint p) {
ctx->drawString(m_string, p, m_inverse);
ctx->drawString(m_string, p, KDColorBlack, KDColorWhite);
}
KDPoint StringLayout::positionOfChild(ExpressionLayout * child) {

View File

@@ -9,7 +9,7 @@ class StringLayout : public ExpressionLayout {
// Here the inverse is a uint8_t instead of a bool, because the size of a bool is
// not standardized, thus since we call a foreign C function with this value we want to be
// sure about compatibility.
StringLayout(const char * string, size_t length, uint8_t inverse=0);
StringLayout(const char * string, size_t length);
~StringLayout();
protected:
void render(KDContext * ctx, KDPoint p) override;
@@ -18,7 +18,6 @@ class StringLayout : public ExpressionLayout {
KDPoint positionOfChild(ExpressionLayout * child) override;
private:
char * m_string;
bool m_inverse;
};
#endif