MCPcopy Create free account
hub / github.com/SFML/SFML / updateRainbowText

Method updateRainbowText

examples/text/Text.cpp:541–592  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

539 }
540
541 void updateRainbowText(float delta)
542 {
543 rainbowColorHue += delta;
544 while (rainbowColorHue >= 360.0f)
545 rainbowColorHue -= 360.0f;
546
547 const auto& shapedGlyphs = text.getShapedGlyphs();
548 auto& vertices = text.getVertexData();
549 const auto hueStep = 360.0f / static_cast<float>(text.getString().getSize());
550 float currentHue = rainbowColorHue;
551 std::uint32_t previousCluster{};
552
553 auto getColor = [&currentHue]()
554 {
555 // Convert hue to RGB using the textbook method
556 const auto h = currentHue / 60.f;
557 const auto x = static_cast<std::uint8_t>(std::round((1.0f - std::abs(std::fmod(h, 2.0f) - 1.0f)) * 255.0f));
558
559 if (h < 1.0f)
560 return sf::Color(255, x, 0);
561 if (h < 2.0f)
562 return sf::Color(x, 255, 0);
563 if (h < 3.0f)
564 return sf::Color(0, 255, x);
565 if (h < 4.0f)
566 return sf::Color(0, x, 255);
567 if (h < 5.0f)
568 return sf::Color(x, 0, 255);
569 return sf::Color(255, 0, x);
570 };
571
572 for (const auto& shapedGlyph : shapedGlyphs)
573 {
574 // Increment hue per cluster
575 if (shapedGlyph.cluster != previousCluster)
576 {
577 currentHue += hueStep;
578 previousCluster = shapedGlyph.cluster;
579 }
580
581 while (currentHue >= 360.0f)
582 currentHue -= 360.0f;
583
584 // Update vertex colors and y position
585 for (auto index = shapedGlyph.vertexOffset; index < shapedGlyph.vertexOffset + shapedGlyph.vertexCount; ++index)
586 {
587 vertices[index].color = getColor();
588 vertices[index].position.y = rainbowYPositions[index] +
589 std::sin(sf::degrees(currentHue * 2.0f).asRadians()) * 2.0f;
590 }
591 }
592 }
593
594 static inline auto currentYPosition = 140.0f;
595 std::unique_ptr<sf::Font> font;

Callers 1

mainFunction · 0.80

Calls 3

ColorClass · 0.50
getSizeMethod · 0.45
getStringMethod · 0.45

Tested by

no test coverage detected